统一ScrollView 滑动键盘收起
2016-12-01 本文已影响97人
hehtao
使用 IQKeyboard 没发现处理ScrollView 滑动时键盘收起的方法,(哪位大神知道还请赐教)于是乎,自己写一个;
直接上代码:
#import <UIKit/UIKit.h>
//=============================== 使用方法: ===================================
// 如果要兼容 IQKeyboard 第三方,请 添加代码: #define KeyboardManager
//==============================================================================
@interface UIScrollView (AutoHideKeyBoard)
/**
* 滑动ScrollView自动收起键盘,默认YES;
*/
@property(nonatomic,assign) BOOL dt_AutoHideKeyBoard;
@end
#ifdef KeyboardManager
#import "IQKeyboardManager.h"
#endif
#import "UIScrollView+AutoHideKeyBoard.h"
#import <objc/runtime.h>
@implementation UIScrollView (AutoHideKeyBoard)
-(void)setDt_AutoHideKeyBoard:(BOOL)dt_AutoHideKeyBoard{
objc_setAssociatedObject(self, @selector(dt_AutoHideKeyBoard), @(dt_AutoHideKeyBoard), OBJC_ASSOCIATION_ASSIGN);
}
-(BOOL)dt_AutoHideKeyBoard{
return [objc_getAssociatedObject(self, @selector(dt_AutoHideKeyBoard)) boolValue];
}
-(UIScrollViewKeyboardDismissMode)keyboardDismissMode{
#ifdef KeyboardManager
if ([[IQKeyboardManager sharedManager] isEnableAutoToolbar]) {
[[IQKeyboardManager sharedManager] resignFirstResponder];
return UIScrollViewKeyboardDismissModeNone;
}else{
return !self.dt_AutoHideKeyBoard ? UIScrollViewKeyboardDismissModeOnDrag : UIScrollViewKeyboardDismissModeNone;
}
#else
return !self.dt_AutoHideKeyBoard ? UIScrollViewKeyboardDismissModeOnDrag : UIScrollViewKeyboardDismissModeNone;
#endif
}
@end