IQKeyboardManager使用起來很方便唱遭,但是也有很多坑:
一浙垫、當一個viewCtrl中禁用IQKeyboardManager上移動的效果時候算行,這個在登錄頁面會常用到;
{
BOOL _wasKeyboardManagerEnabled;
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_wasKeyboardManagerEnabled = [[IQKeyboardManager sharedManager] isEnabled];
[[IQKeyboardManager sharedManager] setEnable:NO];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[IQKeyboardManager sharedManager] setEnable:_wasKeyboardManagerEnabled];
}
2. 若某個類不需要使用 IQKeyboardManager梧油,可以在這個類中這樣設(shè)置,也就是說在這個類中徹底禁用IQKeyboardManager州邢。
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
keyboardManager.enable = NO;
keyboardManager.enableAutoToolbar = NO;
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
keyboardManager.enable = YES;
keyboardManager.enableAutoToolbar = YES;
}
3.項目中使用Masonry儡陨,當一個viewCtrl頁面,但是當你的 backView 【底視圖】不是 tableView 或者scrollView 時量淌。你的導(dǎo)航欄會隨著一起往上跑了骗村。
網(wǎng)上找到方法是
- (void)loadView
{
[super loadView];
self.view = [[UIScrollView alloc] initWithFrame:self.view.bounds];
}
上面這種方法,在沒有使用Masonry時候类少,是可以起作用的叙身,但是在使用Masonry時渔扎,導(dǎo)航欄不會向上跑了硫狞,但是控件的布局亂了。
這是因為在 IQKeyboardManager 和 Masonry 同時使用時晃痴,導(dǎo)航欄上移和make.right
失效的問題引起的残吩。
我也是糾結(jié)好久在這塊
在經(jīng)過多次嘗試之后你會發(fā)現(xiàn)。真正的問題所在是 IQKeyboardManager 和 Masonry 同時使用時倘核,控件放在 scrollView上面泣侮。masonry 的 make.right約束就會失效。但是 make.width等等其他約束還是正常的紧唱。你可以不使用 make.right約束活尊,用 make.width和 make.left代替約束隶校。
但是我覺得還是用 make.right和 make.left約束組合要好些。不要老是寫個 make.width的固定寬度蛹锰。
解決方法:
1.重寫 loadView 方法 深胳。把 self.view 替換成 scrollView。
2.背景容器視圖(back)必須設(shè)置铜犬。而且對 back 約束時 要附帶 make.width.mas_equalTo(self.view);【不寫導(dǎo)致 textField 布局的 make.right 失效】
3.子控件要直接放在self.view 上舞终。不能放在背景容器視圖(back)上面⊙⒒【放在 back上時會無法點擊敛劝,無法成為第一響應(yīng)】
#pragma mark - step 01
-(void)loadView { //不將 self.view 替換成 scrollView 會在點擊底部輸入框時 導(dǎo)航欄也一起往上跑。
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[scrollView setBackgroundColor:[UIColor grayColor]];
self.view = scrollView;
}
/**
1.重寫 loadView 方法 纷宇。把 self.view 替換成 scrollView夸盟。
2.背景容器視圖(back)必須設(shè)置。而且對 back 約束時 要附帶 make.width.mas_equalTo(self.view);
【不寫導(dǎo)致 textField 布局的 make.right 失效】
3.子控件要直接放在self.view 上像捶。不能放在背景容器視圖(back)上面满俗。
【放在 back上時會無法點擊,無法成為第一響應(yīng)】
*/
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"我是導(dǎo)航欄";
#pragma mark - step 02
UIView *back = [[UIView alloc] init];
[self.view addSubview:back];
[back mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
make.width.mas_equalTo(self.view);
//此處必填 - 【關(guān)鍵點】 作岖。不寫導(dǎo)致 textField 布局的 make.right 失效唆垃。
//(但是布局textField 時使用 make.width不受這句話限制。)
}];
UITextField *textView = [[UITextField alloc]initWithFrame:CGRectMake(0, 300, 100, 60)];
textView.backgroundColor = [UIColor redColor];
#pragma mark - step 03
[self.view addSubview:textView];
// [back addSubview:textView];
// textView 放在 back上時會無法點擊痘儡,無法成為第一響應(yīng)辕万。
[textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(20);
make.right.equalTo(self.view).offset(-20);
make.top.equalTo(self.view).offset(250);
make.height.equalTo(@30);
}];
}
4. IQKeyBoardManager按鈕"Done"改成"完成"
IQUIView+IQKeyboardToolbar.m文件里面,找到這一個沉删,替換成initWIthTitle這個方法
// IQBarButtonItem *doneButton =[[IQBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:target action:doneAction];
IQBarButtonItem *doneButton =[[IQBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:target action:doneAction];
注意:,要替換2個地方渐尿,一個是textfid,一個是textView
5. IQKeyBoardManager中,“完成” 按鈕的監(jiān)聽事件
[_textFid addDoneOnKeyboardWithTarget:self action:@selector(btnSearchClick)];
6.如果你不想添加一個特定的自動工具欄在鍵盤上方矾瑰,應(yīng)該添加一個類作為它的工具欄砖茸,代碼如下:
textField.inputAccessoryView = [[UIView alloc] init];
常用屬性介紹
-
sharedManager
:獲取類庫的單例變量 -
enable
:項目使用不使用 IQKeyboardManager 這個類庫,當然殴穴,某些頁面可以根據(jù)需要單獨設(shè)置 -
shouldResignOnTouchOutside
:點擊背景頁面時是否收起鍵盤 -
shouldToolbarUsesTextFieldTintColor
:控制鍵盤上的工具條文字顏色是否用戶自定義凉夯,默認為 NO -
toolbarManageBehaviour
:有多個輸入框時,可以通過點擊Toolbar 上的“前一個” “后一個”按鈕來實現(xiàn)移動到不同的輸入框 -
enableAutoToolbar
:是否顯示鍵盤上的工具條 -
shouldShowTextFieldPlaceholder
:是否顯示占位文字(如果輸入框有占位文字采幌,那么在 Toolbar 中默認會顯示出來) -
placeholderFont
:占位文字的字體大小 -
keyboardDistanceFromTextField
:輸入框距離鍵盤的距離
用法如下:
IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
//控制整個功能是否啟用劲够。
manager.enable = YES;
//控制點擊背景是否收起鍵盤
manager.shouldResignOnTouchOutside = YES;
//控制鍵盤上的工具條文字顏色是否用戶自定義。 注意這個顏色是指textfile的tintcolor
manager.shouldToolbarUsesTextFieldTintColor = YES;
//中間位置是否顯示占位文字
manager.shouldShowTextFieldPlaceholder = YES;
//設(shè)置占位文字的字體
manager.placeholderFont = [UIFont boldSystemFontOfSize:17];
//控制是否顯示鍵盤上的工具條休傍。
manager.enableAutoToolbar = YES;
//某個類中禁止使用工具條
[[IQKeyboardManager sharedManager]disableToolbarInViewControllerClass:[UIViewController class]];