第3方键盘管理库_IQKeyboardManager_Github地址
第1步:在Podfile中添加, 然后终端 pod update
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '7.0'
pod 'AFNetworking', '~> 2.5'
pod 'SDWebImage', '~> 3.7.3'
pod 'MWPhotoBrowser', '~> 2.1.1'
pod 'pop', '~> 1.0.8'
pod 'IQKeyboardManager'
第2步:在AppDelegate中开启键盘管理功能
#import "KeyboardManager.h"
- (void)keyboard
{
IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
// 开启
manager.enable = YES;
// 点击空白区域 自动退出键盘
manager.shouldResignOnTouchOutside = YES;
// 键盘的工具条上: 前后箭头+完成按钮
manager.enableAutoToolbar = YES;
}
tips_1:将Done修改为"完成"
搜索文本【initWithBarButtonSystemItem:UIBarButtonSystemItemDone】,找到IQUIView+IQKeyboardToolbar.h,为啥为直接搜索『"Done"』,因为搜索不到...
//IQBarButtonItem *doneButton =[[IQBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:target action:doneAction];
IQBarButtonItem *doneButton =[[IQBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:target action:doneAction];
tips_2:将每个输入框的Return Key事件转为「tool bar上面的Next事件」
注意: xib中 输入框拖入的先后顺序,对转换后的return key事件有影响
#import "SGForgetCtrl.h"
#import "UIView+Frame.h"
#import "IQKeyboardReturnKeyHandler.h"
@interface SGForgetCtrl ()
{
IQKeyboardReturnKeyHandler *returnKeyHandler;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"取回密码";
[self addBackBtn];
returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
}
tips_3:为某一个控制器禁用键盘管理(虽然极少有这种情况)
tips_4:定制toolBar上的完成事件(不是默认的退出键盘,而是执行相应的期望事件)