這篇文章解決的一個(gè)開發(fā)中的實(shí)際問題就是:當(dāng)彈出鍵盤時(shí),自定義鍵盤上方的view。目前就我的經(jīng)驗(yàn)來看,有兩種解決方法。一個(gè)就是利用UITextField或者UITextView的inputAccessoryView屬性,另一種,就是監(jiān)聽鍵盤彈出的notification來自己解決相關(guān)視圖的位置問題。
第一種解決方法相對(duì)比較簡(jiǎn)單,第二種的方法中有一個(gè)難題就是當(dāng)鍵盤的輸入方式,也就是中英文切換時(shí),鍵盤的高度是會(huì)發(fā)生變化的。需要?jiǎng)討B(tài)來調(diào)整相關(guān)視圖的位置。下面開始詳細(xì)介紹解決方法。
設(shè)定inputAccessoryView屬性
UITextField或者UITextView有一個(gè)inputAccessoryView的屬性,其類型是UIView。使用中,可以自定義一個(gè)view,并將這個(gè)view傳遞給inputAccessoryView的屬性即可。這種實(shí)現(xiàn)方式相對(duì)簡(jiǎn)單,可以滿足很多情況的需求了。下面給出一些示例代碼。
// 新建一個(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];
上面代碼很簡(jiǎn)單,一看就明白了。這里的鍵盤時(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(changeContentViewPoint:) name:UIKeyboardWillShowNotification object:nil];
相關(guān)的函數(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)的。
封裝的小Demon可以直接拿來用,稍微修改一下傳入?yún)?shù)類型執(zhí)行此方法即可
- (void)addTopViewForKeyBoard:(UITextView *)addToView{
// 如果是textFiled添加則將此方法改為- (void)addTopViewForKeyBoard:(UITextFiled *)addToView即可
// 給鍵盤添加導(dǎo)航條
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 10, 320, 40)];
[topView setBarStyle:UIBarStyleBlack];
// 評(píng)論間隙
UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"輸入合作內(nèi)容"
style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
// 完成按鈕
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成"
style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];
[topView setItems:buttonsArray];
[addToView setInputAccessoryView:topView];
}
_textView是需要添加視圖的文字輸入控件
// 收縮鍵盤
-(void)dismissKeyBoard
{
[_textView resignFirstResponder];
}