UIScrollerView添加滑动点击收起键盘方法

1、拖动tableview时隐藏键盘

1.1 UIScrollViewKeyboardDismissMode属性

IOS7后系统提供了一个 UIScrollViewKeyboardDismissMode属性,所有继承于UIScrollView的类皆可以使用。

UIScrollerView添加滑动点击收起键盘方法_第1张图片

该属性有三种类型如下:

typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
    UIScrollViewKeyboardDismissModeNone,
    UIScrollViewKeyboardDismissModeOnDrag,      // dismisses the keyboard when a drag begins
    UIScrollViewKeyboardDismissModeInteractive, // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
} NS_ENUM_AVAILABLE_IOS(7_0);

翻译水平不好,直接看英文吧。英文更切合实意。

1.2 使用方法

self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

它的值除了UIScrollViewKeyboardDismissModeNone,
还有一种是UIScrollViewKeyboardDismissModeInteractive,表示键盘可以随着手指下滑而移出屏幕,具体效果小伙伴们可以自己试着体验下.
既然是UIScrollView的属性,那么在文字常用的UITextView等控件中也可以使用.

2、 点击tableview时隐藏键盘

UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commentTableViewTouchInSide)];
tableViewGesture.numberOfTapsRequired = 1;
tableViewGesture.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:tableViewGesture];

- (void)commentTableViewTouchInSide{
    [self.searchView.inputEdit resignFirstResponder];
}

cancelsTouchesInView初见

你可能感兴趣的:(UIScrollerView添加滑动点击收起键盘方法)