萌萌的小介绍
有点时候吧,你得让用户输入点什么东西,然后用户就点了一下文本框.也许是UITextField
,也许是UITextView
.那个有的时候吧,一下子就TM把输入框档上了😈...或者有键盘的时候你的tableView
滚不上去啊( ⊙ o ⊙ )啊!!!!
这个时候,你还不想敲代码...肿么办呢...然后你就这么做了一下...
pod 'IQKeyboardManager'
万事大吉😀~~
功能小介绍
既然人家是外国的大牛牛写的,那我就翻译一下里面的功能吧<.....>
额😝...
1.是否需要弹出键盘上的ToolBar
[IQKeyboardManager sharedManager].enableAutoToolbar = NO;
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];
}
源码小解析
为神马可以导入之后一句话也不用写呢
因为人家已经在+ (void)load;
方法里面家在过了.而+ (void)load;
方法在类或者分类被加载到运行时的时候就已经被调用了,所以你只要把这个.m文件编译进去,就相当于默认执行了这个方法.
+(void)load
{
//Enabling IQKeyboardManager.
[[IQKeyboardManager sharedManager] setEnable:YES];
}
当然,这个也是浮云,比较low,关键还是要看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];
首先是监听了系统的四类的通知,分别是(1)键盘弹出隐藏.(2)textField开始结束编辑.(3)textView开始结束编辑.(4)屏幕旋转的通知
然后就可以根据这几个状态的变化,在不同的时机做自己喜欢做的事情啦
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
_tapGesture.cancelsTouchesInView = NO;
[_tapGesture setDelegate:self];
_tapGesture.enabled = _shouldResignOnTouchOutside;
这里加入了一个轻敲手势,点击任意视图的地方会结束编辑,收起键盘._tapGesture.cancelsTouchesInView = NO;
这句话的意思是,假如你的界面上有按钮之类的东西,点击按钮键盘不会退出,加上这句话就可以退出啦...
// 需要让屏幕滚动才能让输入源可见时,设置滚动动画执行的时间
_animationDuration = 0.25;
// 要让屏幕滚动才能让输入源可见时,设置滚动动画的方式
_animationCurve = UIViewAnimationCurveEaseInOut;
// 设置输入源底部距离键盘顶部的距离,默认为10
[self setKeyboardDistanceFromTextField:10.0];
// 用户是否可以点击IQKeyboardManager自带的toolbar中的上一个、下一个和完成按钮
[self setShouldPlayInputClicks:NO];
// 默认情况下点击键盘外面部分不收起键盘
[self setShouldResignOnTouchOutside:NO];
// 是否让IQKeyboardManager覆盖用户设置的输入源弹出键盘样式,并设置默认键盘样式
[self setOverrideKeyboardAppearance:NO];
[self setKeyboardAppearance:UIKeyboardAppearanceDefault];
// 默认让没有toolbar的键盘使用默认的toolbar
[self setEnableAutoToolbar:YES];
// 解决滚动过高导致界面和键盘之间有黑色区域
[self setPreventShowingBottomBlankSpace:NO];
// toolbar中间的文字是否在输入源是UITextField时显示UITextField的占位字符
[self setShouldShowTextFieldPlaceholder:YES];
// 设置所有的输入源
[self setToolbarManageBehaviour:IQAutoToolbarBySubviews];
// 是否重新更新布局
[self setLayoutIfNeededOnUpdate:NO];
// 强制禁用/开启IQKeyboardManager的控制器,即便设置了enable
_disabledClasses = [[NSMutableSet alloc] initWithObjects:[UITableViewController class], nil];
_disabledToolbarClasses = [[NSMutableSet alloc] init];
// 添加tableView和collectionView
_toolbarPreviousNextConsideredClass = [[NSMutableSet alloc] initWithObjects:[UITableView class],[UICollectionView class], nil];