當彈出鍵盤時,自定義鍵盤上方的view,有三種解決辦法:一個就是利用UITextField或者UITextView的inputAccessoryView屬性,另一種,就是監聽鍵盤彈出的notification來自己解決相關視圖的位置問題。還有一種是覆蓋 TextFileld 的一些方法。
第一種解決方法相對比較簡單,第二種的方法中有一個難題就是當鍵盤的輸入方式,也就是中英文切換時,鍵盤的高度是會發生變化的。需要動態來調整相關視圖的位置。
設定inputAccessoryView屬性
UITextField或者UITextView有一個inputAccessoryView的屬性,其類型是UIView。使用中,可以自定義一個view,並將這個view傳遞給inputAccessoryView的屬性即可。這種實現方式相對簡單,可以滿足很多情況的需求了。下面給出一些示例代碼。
// 新建一個UITextField,位置及背景顏色隨意寫的。 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 10, 200, 20)]; textField.backgroundColor = [UIColor grayColor]; [self.view addSubview:textField]; // 自定義的view UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)]; customView.backgroundColor = [UIColor lightGrayColor]; textField.inputAccessoryView = customView; // 往自定義view中添加各種UI控件(以UIButton為例) UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 5, 60, 20)]; btn.backgroundColor = [UIColor greenColor]; [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside]; [customView addSubview:btn];
上面代碼很簡單,一看就明白了。這里的鍵盤時通過UITextField的becomeFirstResponder后彈出的。而我在開發中就碰到了一種情況,就是需要通過點擊一個按鈕來彈出鍵盤,同時鍵盤上方的自定義視圖中需要包含一個UITextView。這時,這種情況就不適用了。需要用到第二種方法。
監聽鍵盤事件動態改變自定義view位置
這種方法的思路就是首先自己寫一個view,然后監聽鍵盤的事件,得到鍵盤的位置后調整自己寫的view的位置,保證這個view的下邊界與鍵盤的上邊界相接。在自定義view中包含一個UITextField或者UITextView。通過代碼調用其becomeFirstResponder方法來彈出鍵盤。
下面寫一些關鍵代碼,其中自定義的view名為_mainView,全局變量。
監聽鍵盤事件代碼:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPosition:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPosition:) name:UIKeyboardWillHideNotification object:nil];
移除監聽
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotificati
事件處理函數
// 根據鍵盤狀態,調整_mainView的位置 - (void) changeContentViewPoint:(NSNotification *)notification{ NSDictionary *userInfo = [notification userInfo]; NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGFloat keyBoardEndY = value.CGRectValue.origin.y; // 得到鍵盤彈出后的鍵盤視圖所在y坐標 NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; // 添加移動動畫,使視圖跟隨鍵盤移動 [UIView animateWithDuration:duration.doubleValue animations:^{ [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationCurve:[curve intValue]]; _mainView.center = CGPointMake(_mainView.center.x, keyBoardEndY - STATUS_BAR_HEIGHT - _mainView.bounds.size.height/2.0); // keyBoardEndY的坐標包括了狀態欄的高度,要減去 }]; }
其中添加了一個動畫,使得過渡效果好一點。 mainView中即可添加自定義的UI控件。注意,這個mainView中控件要從最下面開始布局,因為上述代碼是以下方為准的。
一開始,我也選擇了第二種,可是當點擊的時候,會發生延遲現象,還有救是中英文的問題,后來查資料,找到了第三種解決辦法。
覆蓋 UITextField 的方法
UITextField 的方法有如下:
@interface UIView (UITextField) - (BOOL)endEditing:(BOOL)force; // use to make the view or any subview that is the first responder resign (optionally force) @end @protocol UITextFieldDelegate <NSObject> @optional - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing. - (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder - (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end - (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text - (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications) - (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore. @end UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification; UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification; UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;
以下是我寫的代碼
//textField 變成第一響應着,意味着點擊了 textField,然后改變textFieldView 的位置坐標
- (void)textFieldDidBeginEditing:(UITextField *)textField { CGRect rect = self.textFieldView.frame; rect.origin.y = self.view.frame.size.height - 216 - 10; NSTimeInterval animationDuration = 0.3f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; self.textFieldView.frame = rect; [UIView commitAnimations]; }
//點擊虛擬鍵盤上的 return 按鈕后 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self.textField resignFirstResponder]; return YES; } //對textFieldView 的位置調整,並返回 YES,從來返回 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { CGRect rect = self.textFieldView.frame; self.textFieldView.frame = CGRectMake(0, self.view.frame.size.height - 49 + 10, rect.size.width, rect.size.height); return YES; }
這樣就能改變了,其中前兩種方法為拷貝,尊重作者權利,轉載。
http://webfrogs.me/2013/01/09/ios-keyboard-custom-upperview/