Files
knowledge-kit/Chapter1 - iOS/1.23.md
2020-02-25 17:46:51 +08:00

27 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# UIScrollView 拖拽滑动时收起键盘
> 当一个页面的 UIScrollView/UITableView 上有输入框时,为了较好的体验,就是当滑动的时候需要回收键盘
* 最开始的做法是设置 UIScrollView 的代理位当前控制器,监听 scrollViewWillBeginDragging 方法,找到 keyWindow 并且 endEditing
```
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[[UIApplication sharedApplication].keyWindow endEditing:YES];
}
```
* 之后偶然有幸看到一个 UIScrollView 的属性"keyboardDismissModel"。实现上述需求只需要一行代码
```
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
```
* keyboardDismissMode 有3个枚举值
* UIScrollViewKeyboardDismissModeNone默认值也就是拖拽时对于键盘没有任何影响。
* UIScrollViewKeyboardDismissModeOnDragdismisses the keyboard when a drag begins当刚拖拽的时候就会回收键盘
* UIScrollViewKeyboardDismissModeInteractivethe keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss当向下滑动的时候键盘会跟随手势一起下滑当向上滑动的时候键盘也会跟随手势向上滑动而出现。