萌萌的小介紹
有點(diǎn)時(shí)候吧,你得讓用戶(hù)輸入點(diǎn)什么東西,然后用戶(hù)就點(diǎn)了一下文本框.也許是UITextField
,也許是UITextView
.那個(gè)有的時(shí)候吧,一下子就TM把輸入框檔上了??...或者有鍵盤(pán)的時(shí)候你的tableView
滾不上去啊( ⊙ o ⊙ )敖ㄌ薄!!!!
這個(gè)時(shí)候,你還不想敲代碼...腫么辦呢...然后你就這么做了一下...
pod 'IQKeyboardManager'
萬(wàn)事大吉??~~
功能小介紹
既然人家是外國(guó)的大牛牛寫(xiě)的,那我就翻譯一下里面的功能吧<.....>
額??...
1.是否需要彈出鍵盤(pán)上的ToolBar
[IQKeyboardManager sharedManager].enableAutoToolbar = NO;
2.點(diǎn)擊鍵盤(pán)外的部分收起鍵盤(pán)
[IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
3.某個(gè)控制器禁用IQKeyboardManager
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[IQKeyboardManager sharedManager] setEnable:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[IQKeyboardManager sharedManager] setEnable:YES];
}
源碼小解析
為神馬可以導(dǎo)入之后一句話(huà)也不用寫(xiě)呢
因?yàn)槿思乙呀?jīng)在+ (void)load;
方法里面家在過(guò)了.而+ (void)load;
方法在類(lèi)或者分類(lèi)被加載到運(yùn)行時(shí)的時(shí)候就已經(jīng)被調(diào)用了,所以你只要把這個(gè).m文件編譯進(jìn)去,就相當(dāng)于默認(rèn)執(zhí)行了這個(gè)方法.
+(void)load
{
//Enabling IQKeyboardManager.
[[IQKeyboardManager sharedManager] setEnable:YES];
}
當(dāng)然,這個(gè)也是浮云,比較low,關(guān)鍵還是要看init
方法:
初始化方法
// Registering for keyboard notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
// Registering for textField notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldViewDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldViewDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil];
// Registering for textView notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
// Registering for orientation changes notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willChangeStatusBarOrientation:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
首先是監(jiān)聽(tīng)了系統(tǒng)的四類(lèi)的通知,分別是(1)鍵盤(pán)彈出隱藏.(2)textField開(kāi)始結(jié)束編輯.(3)textView開(kāi)始結(jié)束編輯.(4)屏幕旋轉(zhuǎn)的通知
然后就可以根據(jù)這幾個(gè)狀態(tài)的變化,在不同的時(shí)機(jī)做自己喜歡做的事情啦
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
_tapGesture.cancelsTouchesInView = NO;
[_tapGesture setDelegate:self];
_tapGesture.enabled = _shouldResignOnTouchOutside;
這里加入了一個(gè)輕敲手勢(shì),點(diǎn)擊任意視圖的地方會(huì)結(jié)束編輯,收起鍵盤(pán)._tapGesture.cancelsTouchesInView = NO;
這句話(huà)的意思是,假如你的界面上有按鈕之類(lèi)的東西,點(diǎn)擊按鈕鍵盤(pán)不會(huì)退出,加上這句話(huà)就可以退出啦...
// 需要讓屏幕滾動(dòng)才能讓輸入源可見(jiàn)時(shí),設(shè)置滾動(dòng)動(dòng)畫(huà)執(zhí)行的時(shí)間
_animationDuration = 0.25;
// 要讓屏幕滾動(dòng)才能讓輸入源可見(jiàn)時(shí)米碰,設(shè)置滾動(dòng)動(dòng)畫(huà)的方式
_animationCurve = UIViewAnimationCurveEaseInOut;
// 設(shè)置輸入源底部距離鍵盤(pán)頂部的距離此熬,默認(rèn)為10
[self setKeyboardDistanceFromTextField:10.0];
// 用戶(hù)是否可以點(diǎn)擊IQKeyboardManager自帶的toolbar中的上一個(gè)释液、下一個(gè)和完成按鈕
[self setShouldPlayInputClicks:NO];
// 默認(rèn)情況下點(diǎn)擊鍵盤(pán)外面部分不收起鍵盤(pán)
[self setShouldResignOnTouchOutside:NO];
// 是否讓IQKeyboardManager覆蓋用戶(hù)設(shè)置的輸入源彈出鍵盤(pán)樣式泊藕,并設(shè)置默認(rèn)鍵盤(pán)樣式
[self setOverrideKeyboardAppearance:NO];
[self setKeyboardAppearance:UIKeyboardAppearanceDefault];
// 默認(rèn)讓沒(méi)有toolbar的鍵盤(pán)使用默認(rèn)的toolbar
[self setEnableAutoToolbar:YES];
// 解決滾動(dòng)過(guò)高導(dǎo)致界面和鍵盤(pán)之間有黑色區(qū)域
[self setPreventShowingBottomBlankSpace:NO];
// toolbar中間的文字是否在輸入源是UITextField時(shí)顯示UITextField的占位字符
[self setShouldShowTextFieldPlaceholder:YES];
// 設(shè)置所有的輸入源
[self setToolbarManageBehaviour:IQAutoToolbarBySubviews];
// 是否重新更新布局
[self setLayoutIfNeededOnUpdate:NO];
// 強(qiáng)制禁用/開(kāi)啟IQKeyboardManager的控制器绘趋,即便設(shè)置了enable
_disabledClasses = [[NSMutableSet alloc] initWithObjects:[UITableViewController class], nil];
_disabledToolbarClasses = [[NSMutableSet alloc] init];
// 添加tableView和collectionView
_toolbarPreviousNextConsideredClass = [[NSMutableSet alloc] initWithObjects:[UITableView class],[UICollectionView class], nil];