當(dāng)彈出鍵盤時(shí)被因,自定義鍵盤上方的view贡翘,有三種解決辦法:
一個(gè)就是利用UITextField或者UITextView的inputAccessoryView屬性著角。
另一種事富,就是監(jiān)聽鍵盤彈出的notification來自己解決相關(guān)視圖的位置問題技俐。
還有一種是覆蓋 TextFileld 的一些方法。
第一種解決方法相對(duì)比較簡單统台,第二種的方法中有一個(gè)難題就是當(dāng)鍵盤的輸入方式雕擂,也就是中英文切換時(shí),鍵盤的高度是會(huì)發(fā)生變化的贱勃。需要?jiǎng)討B(tài)來調(diào)整相關(guān)視圖的位置捂刺。設(shè)定inputAccessoryView屬性UITextField或者UITextView有一個(gè)inputAccessoryView的屬性,其類型是UIView募寨。使用中族展,可以自定義一個(gè)view,并將這個(gè)view傳遞給inputAccessoryView的屬性即可拔鹰。這種實(shí)現(xiàn)方式相對(duì)簡單仪缸,可以滿足很多情況的需求了。下面給出一些示例代碼列肢。
// 新建一個(gè)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];
上面代碼很簡單瓷马,一看就明白了拴还。這里的鍵盤時(shí)通過UITextField的becomeFirstResponder后彈出的。而我在開發(fā)中就碰到了一種情況欧聘,就是需要通過點(diǎn)擊一個(gè)按鈕來彈出鍵盤片林,同時(shí)鍵盤上方的自定義視圖中需要包含一個(gè)UITextView。這時(shí)怀骤,這種情況就不適用了费封。
需要用到第二種方法。監(jiān)聽鍵盤事件動(dòng)態(tài)改變自定義view位置這種方法的思路就是首先自己寫一個(gè)view蒋伦,然后監(jiān)聽鍵盤的事件弓摘,得到鍵盤的位置后調(diào)整自己寫的view的位置,保證這個(gè)view的下邊界與鍵盤的上邊界相接痕届。在自定義view中包含一個(gè)UITextField或者UITextView韧献。通過代碼調(diào)用其becomeFirstResponder方法來彈出鍵盤末患。下面寫一些關(guān)鍵代碼,其中自定義的view名為_mainView锤窑,全局變量璧针。
監(jiān)聽鍵盤事件代碼:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPosition:) name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self? selector:@selector(changeContentViewPosition:)? name:UIKeyboardWillHideNotification object:nil];
移除監(jiān)聽
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotificatition object:nil];
事件處理函數(shù)
// 根據(jù)鍵盤狀態(tài),調(diào)整_mainView的位置
- (void) changeContentViewPoint:(NSNotification *)notification{? ?
????NSDictionary *userInfo = [notification userInfo];? ?
?????NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];? ?
?????CGFloat keyBoardEndY = value.CGRectValue.origin.y;??
????// 得到鍵盤彈出后的鍵盤視圖所在y坐標(biāo)?
????NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];?
????NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];?
????// 添加移動(dòng)動(dòng)畫果复,使視圖跟隨鍵盤移動(dòng)?
????[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的坐標(biāo)包括了狀態(tài)欄的高度陈莽,要減去 }];
}
其中添加了一個(gè)動(dòng)畫渤昌,使得過渡效果好一點(diǎn)虽抄。 mainView中即可添加自定義的UI控件。注意独柑,這個(gè)mainView中控件要從最下面開始布局迈窟,因?yàn)樯鲜龃a是以下方為準(zhǔn)的。
一開始忌栅,我也選擇了第二種车酣,可是當(dāng)點(diǎn)擊的時(shí)候,會(huì)發(fā)生延遲現(xiàn)象索绪,還有救是中英文的問題湖员,后來查資料,找到了第三種解決辦法瑞驱。覆蓋 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@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 變成第一響應(yīng)著娘摔,意味著點(diǎn)擊了 textField,然后改變textFieldView 的位置坐標(biāo)
- (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];
}
//點(diǎn)擊虛擬鍵盤上的 return 按鈕后
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[self.textField resignFirstResponder];
return YES;
}
//對(duì)textFieldView 的位置調(diào)整唤反,并返回 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;
}