iOS開發(fā)第三方鍵盤處理實(shí)例代碼
最近項(xiàng)目中遇到了鍵盤處理通知被調(diào)用多次的情況,廢了好半天時(shí)間才找到解決辦法,今天就給小伙伴兒們嘮嘮第三方鍵盤處理的那些坑!
詳情請(qǐng)看:『https://github.com/boai/BAKeyboardDemo』 !
1、聊天評(píng)論框的封裝
先聊聊我項(xiàng)目中遇到的奇葩情況吧,一個(gè)直播界面,上面播放器,下面是分段控制器5個(gè)button,5個(gè)界面,其中三個(gè)界面最下面都是評(píng)論框,所以就封裝了一個(gè)評(píng)論框公用。
但是本來用的『IQKeyboardManager』,開源鍵盤處理框架,可是在同一個(gè)界面有多個(gè)評(píng)論框就出現(xiàn)問題了。
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、具體解決辦法
但是我項(xiàng)目中得復(fù)雜情況就不行了,鍵盤彈起異常,收回也異常,尤其是用了聊天室這種view,處理的更麻煩,不要急,看看博愛這些年踩過的坑吧:
先看看鍵盤處理事件吧:
- 1、首先注冊(cè)通知: - (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; /*! 第三方鍵盤回調(diào)三次問題,監(jiān)聽僅執(zhí)行最后一次 */ 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; }]; } /*! * 獲取當(dāng)前View的VC * * @return 獲取當(dāng)前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; }
具體情況是這樣的,在測(cè)試過程中,其他界面的評(píng)論框都沒問題,就直播這個(gè)VC有問題,就一步步往下找,后來發(fā)現(xiàn):iOS的第三方鍵盤會(huì)在【- (void)keyboardWasShown:(NSNotification *)notification】這個(gè)方法中來回調(diào)用多次,不止三次好像,然后就想到一個(gè)辦法,
/*! 第三方鍵盤回調(diào)三次問題,監(jiān)聽僅執(zhí)行最后一次 */ 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; }]; }
在這里處理這個(gè)鍵盤彈起事件中第一次獲取鍵盤的高度,然后就直接把上面的view給彈上去,這樣就避免了第三方鍵盤會(huì)來回調(diào)用多次方法,造成鍵盤彈起異常的問題就迎刃而解了!
4、如果這樣還不能解決你的鍵盤問題,還有中萬能方法:
平時(shí)可能遇到這種需求:點(diǎn)擊一個(gè)按鈕,彈出評(píng)論框和鍵盤,這時(shí)你就需要這樣處理了:
1、創(chuàng)建一個(gè) TextField、TextField2,把TextField位置放到屏幕外面看不到的地方,TextField 有個(gè)屬性,用法如下:
self.replyTextField.inputAccessoryView = self.replyTextField2;
需要添加target 事件:
[self.replyTextField addTarget:self action:@selector(replyTextFieldChanged:) forControlEvents:UIControlEventEditingChanged];
事件方法處理:
- (void)replyTextFieldChanged:(UITextField *)textField { NSLog(@"textFieldShouldBeginEditing輸入內(nèi)容****:%@", textField.text); if (textField != self.replyTextField2) { self.replyTextField2.text = textField.text; } NSLog(@"textFieldShouldBeginEditing輸入內(nèi)容1:%@", self.replyTextField.text); NSLog(@"textFieldShouldBeginEditing輸入內(nèi)容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; }
這樣處理,不管你的鍵盤在哪里,輸入框都會(huì)跟著你的鍵盤走,而且不會(huì)再出現(xiàn)錯(cuò)位,計(jì)算不準(zhǔn)確的地方!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解iOS應(yīng)用使用Storyboard布局時(shí)的IBOutlet與IBAction
這篇文章主要介紹了iOS應(yīng)用使用Storyboard布局時(shí)的IBOutlet與IBAction,文中還附帶講解了為什么IBOutlet屬性是weak的,需要的朋友可以參考下2016-04-04Swift中的HTTP請(qǐng)求體Request Bodies使用示例詳解
這篇文章主要為大家介紹了Swift中的HTTP請(qǐng)求體Request Bodies使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02iOS+PHP注冊(cè)登錄系統(tǒng) iOS部分(下)
這篇文章主要介紹了iOS+PHP注冊(cè)登錄系統(tǒng)的iOS部分,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12iOS實(shí)現(xiàn)封裝一個(gè)獲取通訊錄的工具類詳解
這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)封裝一個(gè)獲取通訊錄的工具類的相關(guān)資料,這是自己平時(shí)封裝的一個(gè)工具類,使用非常方便,文中給出了詳細(xì)的示例代碼,需要的朋友們可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10ios學(xué)習(xí)筆記之基礎(chǔ)數(shù)據(jù)類型的轉(zhuǎn)換
在編碼過程中,數(shù)據(jù)的處理是必要的。眾多數(shù)據(jù)中,NSString、NSData、NSArray、 NSDictionary等數(shù)據(jù)類型是常用的,對(duì)付它們?nèi)菀?,但是在多個(gè)數(shù)據(jù)類型之間轉(zhuǎn)換就需要技巧了。本文主要給大家介紹ios中基礎(chǔ)數(shù)據(jù)類型的轉(zhuǎn)換,有需要的下面來一起看看吧。2016-11-11