iOS學習筆記——鍵盤處理,ios學習筆記鍵盤

來源:互聯網
上載者:User

iOS學習筆記——鍵盤處理,ios學習筆記鍵盤

  在網上找到的資料比較零散,這部分學起來感覺也有點空虛,內容就只包括隱藏鍵盤和鍵盤高度兩部分

    隱藏鍵盤其實就在我學習iOS開發的第一個程式裡面已經實踐過了,不過當時還懵懵懂懂,現在就瞭解了是什麼一回事,就記錄一下,也額外加點內容上去。

    說這個鍵盤的出現和隱藏是和輸入框擷取和失去焦點有關係,輸入框擷取了焦點,軟鍵盤就會出現;輸入框失去了焦點,軟鍵盤就會消失。這個就和Android的有出入。所以要鍵盤消失其實很簡單的一行代碼就可以完成了

[nameTextField resignFirstResponder]; //nameTextFiled就是輸入框的名字

但是在哪個地方執行,觸發機制就多加一點步驟了。讓鍵盤消失首要的肯定是文字框輸入完畢(以按斷行符號鍵為準)就應該消失,那就要去實現UITextFieldDelegate的- (BOOL)textFieldShouldReturn:(UITextField *)textField 方法,並且把輸入框的delegate設定成當前的ViewController。代碼如下

@interface HGTabItem2KeyboardViewController : UIViewController<UITextFieldDelegate>{}@end

 ViewDidLoad的代碼

self.txtTextBox.delegate=self;

 添加方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField{if(self.txtTextBox==textField)[textField resignFirstResponder];return true;} 

在平時使用軟鍵盤的時候總有一個習慣:不是沒次打字都想打完結束的,當打字打到一半然後推出不想打讓軟鍵盤消失的時候,就會點擊一下螢幕空白的地方,軟鍵盤就消失了。現在已經學習了觸控事件的話對這個已經不難了,除了在- (BOOL)textFieldShouldReturn:(UITextField *)textField 方法裡面調用resignFirstResponder方法外,還在觸控的事件裡也調用self.view endEditing就行了,這個觸控事件可以使用touchesEnded:withEvent:,也可以使用UITapGestureRecognizer 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{  [self.view endEditing:true];}

 聲明代碼

@interface HGTabItem2KeyboardViewController : UIViewController<UITextFieldDelegate,UIGestureRecognizerDelegate>{}@end

ViewDidLoad中的代碼

UITapGestureRecognizer *gestrue=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];gestrue.numberOfTouchesRequired=1;[self.view addGestureRecognizer:gestrue];

 添加方法

-(void)singleTap:(UITapGestureRecognizer *)gecognizer{  [self.txtTextBox endEditing:true];}

處理鍵盤高度實際上是利用了鍵盤的全域事件,網上有種說法是在iOS5.0以後,鍵盤會在不同的輸入語言下高度會有所變化,但經我在模擬器上實踐後發現這個高度的差別不存在,但是這個鍵盤高度還是有用的,例如QQ,在鍵盤出現的時候,整個視圖會往上移動,移動的距離就是鍵盤高度,這個就用到了鍵盤高度了。鍵盤高度是用到鍵盤的全域事件

  • UIKeyboardWillShowNotification;
  • UIKeyboardDidShowNotification;
  • UIKeyboardWillHideNotification;
  • UIKeyboardDidHideNotification;

在iOS5.0還增加了兩個事件

  • UIKeyboardWillChangeFrameNotification
  • UIKeyboardDidChangeFrameNotification

以上的事件看名字就知道是在什麼時候觸發,但是ChangeFrame事件的效果不算明顯,當因為當鍵盤高度變化的時候同時觸發了show和ChangeFrame兩個事件,下面還是把代碼粘出來

在ViewDidLoad加入以下方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChange:) name:UIKeyboardDidChangeFrameNotification object:nil];

再加入以下事件方法

- (void)didReceiveMemoryWarning{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.} -(BOOL)textFieldShouldReturn:(UITextField *)textField{if(self.txtTextBox==textField)[textField resignFirstResponder];return true;} -(void)keyboardWillChange:(NSNotification *)notif{NSLog(@"keyboardWillChange");} -(void)keyboardDidChange:(NSNotification*)notif{ } -(void)keyboardWillShow:(NSNotification*)notif
{//keyboard height will be 216, on iOS version older than 5.0CGRect boxFrame = self.view.frame;boxFrame.origin.y = -[[[notif userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height+self.tabBarController.tabBar.frame.size.height;//50;[UIView animateWithDuration:0.3f animations:^{self.view.frame = boxFrame;}];NSLog(@"keyboardWillShow");}-(void)keyboardWillHide:(NSNotification*)notif
{CGRect boxFrame = self.view.frame;boxFrame.origin.y =0; //216;[UIView animateWithDuration:0.3f animations:^{self.view.frame = boxFrame;}];}

在點擊的時候,文字框會上移,但是文字框剛好在鍵盤的頂部,還是在視圖的最底部。當鍵盤隱藏時,文字框剛好下移,最終剛好在螢幕最底端。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.