1、启用IQKeyboardManager
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//默认为YES
[[IQKeyboardManager sharedManager] setEnable:YES];
[[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
return YES;
}
2、键盘弹出时,点击背景,键盘收回
[IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
3、在某个控制器禁用 IQKeyboardManager
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[IQKeyboardManager sharedManager] setEnable:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[IQKeyboardManager sharedManager] setEnable:YES];
}
4、键盘 Return 键的处理
可以将键盘上 Return 键变为 next 键,点击进入下一个输入框,最后一个UITextField/UITextView 的时候变为 Done ,点击收起键盘。
@interface IQKeyboardManagerController (){
IQKeyboardReturnKeyHandler * _returnKeyHander;
}
@end
@implementation IQKeyboardManagerController
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[IQKeyboardManager sharedManager].enable = YES;
_returnKeyHander = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[IQKeyboardManager sharedManager].enable = NO;
}
@end
5、Done 修改为 完成
[IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = "完成";
或者
[IQKeyboardManager sharedManager].toolbarDoneBarButtonItemImage = [UIImage imageNamed:@"IQKeyboardImage"];
6、修改Toolbar颜色
[IQKeyboardManager sharedManager].shouldToolbarUsesTextFieldTintColor = NO;
[IQKeyboardManager sharedManager].toolbarTintColor = [UIColor redColor];
7、设置键盘textField的距离,不能小于零,默认是10.0。<触发条件是textField需要改变y时 >
[IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 10.0f;
8、设置 toolBar 上的 placeholder 是否显示
如果输入框有 placeholder 的话,那么在 toolbar 中默认会显示出来。不想要的话就设置为 NO;
@property(nonatomic, assign) BOOL shouldShowTextFieldPlaceholder;
网友评论