最近公司項(xiàng)目需要實(shí)現(xiàn)一個(gè)類(lèi)似微信朋友圈的點(diǎn)擊某個(gè)按鈕,彈出鍵盤(pán)和一個(gè)自定義的輔助視圖,包括輸入框和按鈕.
1.當(dāng)時(shí)自己的第一個(gè)想法,就是先創(chuàng)建一個(gè)屏幕外的大小為(1,1)的UITextField,通過(guò)點(diǎn)擊按鈕,使這個(gè)textFiled成為第一響應(yīng)者,從而彈出鍵盤(pán).那么問(wèn)題來(lái)了,彈出鍵盤(pán)是容易,但是如何實(shí)現(xiàn)自定義視圖,不用擔(dān)心,系統(tǒng)的UITextfield和UITextView都可以設(shè)置一個(gè)inputAccessoryView(就是鍵盤(pán)上方的視圖).很簡(jiǎn)單,這就實(shí)現(xiàn)了需要完成的功能.結(jié)果,因?yàn)楫?dāng)前頁(yè)面出現(xiàn)兩個(gè)輸入框,對(duì)于成為第一響應(yīng)者,或者取消成為第一響應(yīng)者有比較大的問(wèn)題,導(dǎo)致在某些輸入法帶有收起鍵盤(pán)的按鈕失效,還有會(huì)出現(xiàn),鍵盤(pán)上方視圖,每次點(diǎn)擊都會(huì)切換frame的bug.(原因未明)?
所以我放棄了這種方法.
2.通過(guò)監(jiān)聽(tīng)鍵盤(pán)彈出的通知進(jìn)行相應(yīng)的處理.
1> 自定義一個(gè)View,是帶有輸入框和其他功能視圖的控件,作為鍵盤(pán)的輔助視圖.(代碼如下:)
UIView?*keyBoardTopView?=?[[UIView?alloc]?initWithFrame:CGRectMake(0,?HZKScreenH,?HZKScreenW,?50)];??
keyBoardTopView.backgroundColor?=?[UIColor?whiteColor];??
keyBoardTopView.layer.borderWidth?=?0.7;??
keyBoardTopView.layer.borderColor?=?HZKColor(204,?204,?204).CGColor;??
//發(fā)布按鈕??
self.sendBtn?=?[[UIButton?alloc]?initWithFrame:CGRectMake(keyBoardTopView.bounds.size.width?-?60?-?12,?4,?60,?45)];??
self.sendBtn.titleLabel.font?=?[UIFont?boldSystemFontOfSize:17];??
[self.sendBtn?setTitle:@"發(fā)布"?forState:UIControlStateNormal];??
self.sendBtn.alpha?=?0.4;??
self.sendBtn.tag?=?2002;??
[self.sendBtn?setTitleColor:[UIColor?grayColor]?forState:UIControlStateNormal];??
[self.sendBtn?addTarget:self?action:@selector(dismissKeyboard:)?forControlEvents:UIControlEventTouchUpInside];??
[keyBoardTopView?addSubview:self.sendBtn];??
//輸入框??
UITextField?*inputTF?=?[[UITextField?alloc]?init];??
inputTF.frame?=?CGRectMake(10,?4,?HZKScreenW?-?10?-?72,?42);??
inputTF.placeholder?=?@"請(qǐng)輸入評(píng)論";??
inputTF.tag?=?1001;??
inputTF.layer.cornerRadius?=?5;??
inputTF.layer.masksToBounds?=?YES;??
self.commentTF?=?inputTF;??
inputTF.backgroundColor?=?HZKColor(242,?242,?242);??
[inputTF?addTarget:self?action:@selector(textFieldEditChanged:)?forControlEvents:UIControlEventEditingChanged];??
[keyBoardTopView?addSubview:inputTF];??
self.commentView?=?keyBoardTopView;??
[self.view?addSubview:keyBoardTopView];

設(shè)置這個(gè)View的Y值是屏幕高度,這樣的話,就可以先達(dá)到把這個(gè)View隱藏起來(lái)的目的.然后點(diǎn)擊需要喚起鍵盤(pán)的按鈕,讓這個(gè)View里面的輸入框子控件,成為第一響應(yīng)者.通過(guò)監(jiān)聽(tīng)鍵盤(pán)彈出和收起的兩個(gè)通知,做相應(yīng)的動(dòng)畫(huà)處理(代碼如下)
監(jiān)聽(tīng)鍵盤(pán)的兩個(gè)方法:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
-?(void)keyBoardWillShow:(NSNotification?*)notification??
{??
//?????獲取用戶(hù)信息??
NSDictionary?*userInfo?=?[NSDictionary?dictionaryWithDictionary:notification.userInfo];??
//?獲取鍵盤(pán)高度??
CGRect?keyBoardBounds??=?[[userInfo?objectForKey:UIKeyboardFrameEndUserInfoKey]?CGRectValue];??
CGFloat?keyBoardHeight?=?keyBoardBounds.size.height;??
//?獲取鍵盤(pán)動(dòng)畫(huà)時(shí)間??
CGFloat?animationTime??=?[[userInfo?objectForKey:UIKeyboardAnimationDurationUserInfoKey]?floatValue];??
//?定義好動(dòng)作??
void?(^animation)(void)?=?^void(void)?{??
self.commentView.transform?=?CGAffineTransformMakeTranslation(0,?-(keyBoardHeight?+?50));??
????};??
if?(animationTime?>?0)?{??
[UIView?animateWithDuration:animationTime?animations:animation];??
}else?{??
????????animation();??
????}??
}??
-?(void)keyBoardWillHide:(NSNotification?*)notificaiton??
{??
//?獲取用戶(hù)信息??
NSDictionary?*userInfo?=?[NSDictionary?dictionaryWithDictionary:notificaiton.userInfo];??
//?獲取鍵盤(pán)動(dòng)畫(huà)時(shí)間??
CGFloat?animationTime??=?[[userInfo?objectForKey:UIKeyboardAnimationDurationUserInfoKey]?floatValue];??
//?定義好動(dòng)作??
void?(^animation)(void)?=?^void(void)?{??
self.commentView.transform?=?CGAffineTransformIdentity;??
????};??
if?(animationTime?>?0)?{??
[UIView?animateWithDuration:animationTime?animations:animation];??
}else?{??
????????animation();??
????} }??
這樣的話,就可以擺脫一些不知名的bug出現(xiàn),而且用戶(hù)交互友好.
注意:
在使用鍵盤(pán)監(jiān)聽(tīng)的控制器中,如果項(xiàng)目中有用到IQKeyboardManager,一定要在當(dāng)前控制器,重新設(shè)置這個(gè)第三方庫(kù)為不可用,不然彈出鍵盤(pán)后,你的自定義視圖會(huì)便宜兩個(gè)鍵盤(pán)高度的位置.原因是系統(tǒng)的監(jiān)聽(tīng)和IQKeyboardManager的監(jiān)聽(tīng)沖突了...