最近項目中遇到了鍵盤處理通知被調用多次的情況,廢了好半天時間才找到解決辦法,今天就給小伙伴兒們嘮嘮第三方鍵盤處理的那些坑!
詳情請看:『https://github.com/boai/BAKeyboardDemo』 !
1、聊天評論框的封裝
先聊聊我項目中遇到的奇葩情況吧,一個直播界面,上面播放器,下面是分段控制器5個button,5個界面,其中三個界面最下面都是評論框,所以就封裝了一個評論框公用。
但是本來用的『IQKeyboardManager』,開源鍵盤處理框架,可是在同一個界面有多個評論框就出現問題了。
2、先看看『IQKeyboardManager』的使用吧:
#import "AppDelegate.h"AppDelegate 中添加這段代碼,就可以全局不用管鍵盤的彈起收回了!#pragma mark - 鍵盤處理- (void)completionHandleIQKeyboard{ IQKeyboardManager *manager = [IQKeyboardManager sharedManager]; manager.enable = YES; manager.shouldResignOnTouchOutside = YES; manager.shouldToolbarUsesTextFieldTintColor = YES; manager.enableAutoToolbar = YES;}
3、具體解決辦法
但是我項目中得復雜情況就不行了,鍵盤彈起異常,收回也異常,尤其是用了聊天室這種view,處理的更麻煩,不要急,看看博愛這些年踩過的坑吧:
先看看鍵盤處理事件吧:
- 1、首先注冊通知:- (void)registNotification{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];} - 2、再把后路想好:移除通知- (void)removeNotification{ [[NSNotificationCenter defaultCenter] removeObserver:self];}- 3、通知事件處理:/*! 鍵盤顯示要做什么 */- (void)keyboardWasShown:(NSNotification *)notification{ NSDictionary *info = [notification userInfo]; double duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGFloat curkeyBoardHeight = [[info objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height; CGRect begin = [[info objectForKey:@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue]; CGRect end = [[info objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue]; CGFloat keyBoardHeight; /*! 第三方鍵盤回調三次問題,監聽僅執行最后一次 */ if(begin.size.height > 0 && (begin.origin.y - end.origin.y > 0)) { keyBoardHeight = curkeyBoardHeight; [UIView animateWithDuration:duration animations:^{ CGRect viewFrame = [self getCurrentViewController].view.frame; viewFrame.origin.y -= keyBoardHeight; [self getCurrentViewController].view.frame = viewFrame; }]; }}- (void)keyboardWasHidden:(NSNotification *)notification{ NSDictionary *info = [notification userInfo]; double duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; [UIView animateWithDuration:duration animations:^{ CGRect viewFrame = [self getCurrentViewController].view.frame; viewFrame.origin.y = 0; [self getCurrentViewController].view.frame = viewFrame; }];}/*!* 獲取當前View的VC** @return 獲取當前View的VC*/- (UIViewController *)getCurrentViewController{ for (UIView *view = self; view; view = view.superview) { UIResponder *nextResponder = [view nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) { return (UIViewController *)nextResponder; } } return nil;}
具體情況是這樣的,在測試過程中,其他界面的評論框都沒問題,就直播這個VC有問題,就一步步往下找,后來發現:iOS的第三方鍵盤會在【- (void)keyboardWasShown:(NSNotification *)notification】這個方法中來回調用多次,不止三次好像,然后就想到一個辦法,
/*! 第三方鍵盤回調三次問題,監聽僅執行最后一次 */if(begin.size.height > 0 && (begin.origin.y - end.origin.y > 0)){ keyBoardHeight = curkeyBoardHeight; [UIView animateWithDuration:duration animations:^{ CGRect viewFrame = [self getCurrentViewController].view.frame; viewFrame.origin.y -= keyBoardHeight; [self getCurrentViewController].view.frame = viewFrame; }];}
在這里處理這個鍵盤彈起事件中第一次獲取鍵盤的高度,然后就直接把上面的view給彈上去,這樣就避免了第三方鍵盤會來回調用多次方法,造成鍵盤彈起異常的問題就迎刃而解了!
4、如果這樣還不能解決你的鍵盤問題,還有中萬能方法:
平時可能遇到這種需求:點擊一個按鈕,彈出評論框和鍵盤,這時你就需要這樣處理了:
1、創建一個 TextField、TextField2,把TextField位置放到屏幕外面看不到的地方,TextField 有個屬性,用法如下:
self.replyTextField.inputAccessoryView = self.replyTextField2;
需要添加target 事件:
事件方法處理:
- (void)replyTextFieldChanged:(UITextField *)textField{ NSLog(@"textFieldShouldBeginEditing輸入內容****:%@", textField.text); if (textField != self.replyTextField2) { self.replyTextField2.text = textField.text; } NSLog(@"textFieldShouldBeginEditing輸入內容1:%@", self.replyTextField.text); NSLog(@"textFieldShouldBeginEditing輸入內容2:%@", self.replyTextField2.text);}- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [self.replyTextField resignFirstResponder]; [self.replyTextField2 resignFirstResponder]; [[self getCurrentViewController].view endEditing:YES]; if (self.clickIndexBlock) { self.clickIndexBlock(self.replyTextField2.text); self.replyTextField.text = @""; self.replyTextField2.text = @""; } return YES;}
這樣處理,不管你的鍵盤在哪里,輸入框都會跟著你的鍵盤走,而且不會再出現錯位,計算不準確的地方!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答