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;}];}
在點擊的時候,文字框會上移,但是文字框剛好在鍵盤的頂部,還是在視圖的最底部。當鍵盤隱藏時,文字框剛好下移,最終剛好在螢幕最底端。