在開發(fā)中病曾,可能會遇到一個(gè)頁面好多textField或者textView的情況处面,鍵盤彈出時(shí)如贷,會遮蓋下面的輸入框陷虎,使我們看不到內(nèi)容,以往我都是測量當(dāng)前textfield在屏幕中的位置杠袱,current_y,及current_height尚猿,計(jì)算當(dāng)前鍵盤的height是否比當(dāng)前textfield的下方位置要高,如果高的話楣富,則需調(diào)整self.view整體位置凿掂。詳見文章鏈接? http://www.reibang.com/p/82881c4659b8? 這個(gè)方法比較麻煩,現(xiàn)在我們引入IQKeyboardManger就能輕松解決我們的問題
1 . 首先導(dǎo)入IQKeyboardManager框架
$? pod 'IQKeyboardManager'
2.安裝完成之后,引入文件庄萎,最好放在項(xiàng)目的PrefixHeader文件中
#import "IQKeyboardManager.h"
3.創(chuàng)建如圖1 的? 幾個(gè)textField
輸入文字時(shí)踪少,鍵盤會遮擋部分輸入框
? ? UITextField * tf = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
? ? tf.placeholder = @"please input placeholder words.";
? ? UITextField * tf2 = [[UITextField alloc]initWithFrame:CGRectMake(100, 240, 200, 60)];
? ? tf2.placeholder = @"please input placeholder words.";
? ? UITextField* tf3 = [[UITextFieldalloc]initWithFrame:CGRectMake(100,320,200,60)];
? ? tf3.placeholder = @"please input placeholder words.";
? ? UITextField* tf4 = [[UITextFieldalloc]initWithFrame:CGRectMake(100,400,200,60)];
? ? tf4.placeholder = @"please input placeholder words.";
? ? UITextField * tf5 = [[UITextField alloc]initWithFrame:CGRectMake(100, 480, 200, 60)];
? ? tf5.placeholder = @"please input placeholder words.";
? ? tf.borderStyle = UITextBorderStyleRoundedRect;
? ? tf2.borderStyle = UITextBorderStyleRoundedRect;
? ? tf3.borderStyle = UITextBorderStyleRoundedRect;
? ? tf4.borderStyle = UITextBorderStyleRoundedRect;
? ? tf5.borderStyle = UITextBorderStyleRoundedRect;
? ? [self.viewaddSubview:tf];
? ? [self.viewaddSubview:tf5];
? ? [self.viewaddSubview:tf2];
? ? [self.viewaddSubview:tf3];
? ? [self.viewaddSubview:tf4];
3. 具體設(shè)置如下
//設(shè)置IQKeyboardManager
//1 控制自動鍵盤功能啟用與否,默認(rèn)為YES (只要項(xiàng)目中導(dǎo)入了此框架糠涛,不用設(shè)置下面代碼援奢,也會自動開啟 )
[IQKeyboardManager sharedManager].enable = YES;
//2? 鍵盤彈出時(shí),點(diǎn)擊背景忍捡,鍵盤回收
[IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
//3? 隱藏鍵盤上的toolbar,默認(rèn)是開啟的
[IQKeyboardManager sharedManager].enableAutoToolbar = NO;
//4? 如果某一個(gè)m文本框確實(shí)不需要鍵盤上面的toolbar(置空即可)
? ? tf.inputAccessoryView = [[UIView alloc]init];
//5. 如果某個(gè)頁面一進(jìn)來不想讓鍵盤彈出
- (void)viewWillAppear:(BOOL)animated{
? ? [superviewWillAppear:animated];
? ? //關(guān)閉自動鍵盤功能
? [IQKeyboardManager sharedManager].enable = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
? ? [super viewWillDisappear:animated];
? //開啟自動鍵盤功能
? [IQKeyboardManager sharedManager].enable = YES;
}
結(jié)果如下集漾,鍵盤不會遮擋輸入框,頁面整體自動上調(diào)锉罐,大功告成7瘛!脓规!