【发布时间】:2016-05-10 03:37:32
【问题描述】:
我想添加功能以在用户在键盘外点击时关闭 UITextFields 和 UITextViews 上的键盘。我有一个与 iOS 日历中的新事件非常相似的视图,它是一堆静态表格单元格,其中一些有文本字段,一些发送到显示视图(如日历应用程序中新事件中的“重复”单元格)。
我尝试使用以下内容:
override func viewDidLoad() {
super.viewDidLoad()
//Looks for single or multiple taps.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
//Calls this function when the tap is recognized.
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
但这会破坏点击以转到节目的单元格的功能。我可以通过让键盘按下“返回”来解决这个问题,但我不能用我的UITextView 做到这一点,因为它会添加一个换行符。日历应用程序在似乎向上和向下滑动时关闭了UITextView 中的键盘,但我不确定如何自己添加此功能,因为我是 iOS 编程新手。
有解决这个问题的办法吗?
【问题讨论】:
标签: ios swift uitableview uitextfield uitextview