當(dāng)彈出鍵盤(pán)時(shí),自定義鍵盤(pán)上方的view,有三種解決辦法:
一個(gè)就是利用UITextField或者UITextView的inputAccessoryView屬性。
另一種,就是監(jiān)聽(tīng)鍵盤(pán)彈出的notification來(lái)自己解決相關(guān)視圖的位置問(wèn)題。
還有一種是覆蓋 TextFileld 的一些方法。
第一種解決方法相對(duì)比較簡(jiǎn)單,第二種的方法中有一個(gè)難題就是當(dāng)鍵盤(pán)的輸入方式,也就是中英文切換時(shí),鍵盤(pán)的高度是會(huì)發(fā)生變化的。需要?jiǎng)討B(tài)來(lái)調(diào)整相關(guān)視圖的位置。設(shè)定inputAccessoryView屬性UITextField或者UITextView有一個(gè)inputAccessoryView的屬性,其類(lèi)型是UIView。使用中,可以自定義一個(gè)view,并將這個(gè)view傳遞給inputAccessoryView的屬性即可。這種實(shí)現(xiàn)方式相對(duì)簡(jiǎn)單,可以滿足很多情況的需求了。下面給出一些示例代碼。
// 新建一個(gè)UITextField,位置及背景顏色隨意寫(xiě)的。
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];
上面代碼很簡(jiǎn)單,一看就明白了。這里的鍵盤(pán)時(shí)通過(guò)UITextField的becomeFirstResponder后彈出的。而我在開(kāi)發(fā)中就碰到了一種情況,就是需要通過(guò)點(diǎn)擊一個(gè)按鈕來(lái)彈出鍵盤(pán),同時(shí)鍵盤(pán)上方的自定義視圖中需要包含一個(gè)UITextView。這時(shí),這種情況就不適用了。
需要用到第二種方法。監(jiān)聽(tīng)鍵盤(pán)事件動(dòng)態(tài)改變自定義view位置這種方法的思路就是首先自己寫(xiě)一個(gè)view,然后監(jiān)聽(tīng)鍵盤(pán)的事件,得到鍵盤(pán)的位置后調(diào)整自己寫(xiě)的view的位置,保證這個(gè)view的下邊界與鍵盤(pán)的上邊界相接。在自定義view中包含一個(gè)UITextField或者UITextView。通過(guò)代碼調(diào)用其becomeFirstResponder方法來(lái)彈出鍵盤(pán)。下面寫(xiě)一些關(guān)鍵代碼,其中自定義的view名為_(kāi)mainView,全局變量。
監(jiān)聽(tīng)鍵盤(pá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)聽(tīng)
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotificatition object:nil];
事件處理函數(shù)
// 根據(jù)鍵盤(pán)狀態(tài),調(diào)整_mainView的位置
- (void) changeContentViewPoint:(NSNotification *)notification{? ?
????NSDictionary *userInfo = [notification userInfo];? ?
?????NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];? ?
?????CGFloat keyBoardEndY = value.CGRectValue.origin.y;??
????// 得到鍵盤(pán)彈出后的鍵盤(pán)視圖所在y坐標(biāo)?
????NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];?
????NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];?
????// 添加移動(dòng)動(dòng)畫(huà),使視圖跟隨鍵盤(pán)移動(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)畫(huà),使得過(guò)渡效果好一點(diǎn)。 mainView中即可添加自定義的UI控件。注意,這個(gè)mainView中控件要從最下面開(kāi)始布局,因?yàn)樯鲜龃a是以下方為準(zhǔn)的。
一開(kāi)始,我也選擇了第二種,可是當(dāng)點(diǎn)擊的時(shí)候,會(huì)發(fā)生延遲現(xiàn)象,還有救是中英文的問(wèn)題,后來(lái)查資料,找到了第三種解決辦法。覆蓋 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;
以下是我寫(xiě)的代碼
//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)擊虛擬鍵盤(pán)上的 return 按鈕后
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[self.textField resignFirstResponder];
return YES;
}
//對(duì)textFieldView 的位置調(diào)整,并返回 YES,從來(lái)返回
- (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;
}