关闭 UIScrollView 中的键盘

Dismissing the keyboard in a UIScrollView(关闭 UIScrollView 中的键盘)
本文介绍了关闭 UIScrollView 中的键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

好的,我在 UIScrollView 中有几个 UITextFieldsUITextViews,我想将键盘设置为随时消失scrollview 被触摸或滚动(当然,当您在文本字段/视图内触摸时除外).

Alright, I have a couple of UITextFields and UITextViews inside a UIScrollView, and I'd like to set the keyboard to disappear whenever the scrollview is touched or scrolled (except when you touch down inside the text field/view, of course).

我目前的尝试是将 UIScrollView 替换为子类,并将其设置为在 touchesBegan 内调用 removeKeyboard 函数(在主视图控制器内定义)方法.但是,这只会在正常触摸时移除键盘,而不是在简单地滚动视图时.那么,在 UIScrollView 中移除键盘的最佳方法是什么?

My current attempt at doing this is replacing the UIScrollView with a subclass, and setting it to call a removeKeyboard function (defined inside the main view controller) inside the touchesBegan method. However, this only removes the keyboard for a normal touch, not when the view is simply scrolled. So, what's the best way to remove the keyboard inside a UIScrollView?

提前感谢您的帮助.

推荐答案

这是在 iOS 7.0 及更高版本中实现此目标的最简洁方法.

Here is the cleanest way to achieve this in iOS 7.0 and above.

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

或者

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

在斯威夫特中:

scrollView.keyboardDismissMode = .onDrag

或者

scrollView.keyboardDismissMode = .interactive

这篇关于关闭 UIScrollView 中的键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
Handling scroll views with (custom, interactive) view controller presentation and dismissal(使用(自定义、交互式)视图控制器呈现和解除处理滚动视图)