如标题所述,我有兴趣在搜索栏外单击时从搜索栏中解除键盘。类似的问题在touchesBegan中使用了self.view.endEditing(true),但我没有运气。我不确定我是否遗漏了其他任何东西,或者我是否正确设置了我的搜索栏。
INNER JOIN
答案 0 :(得分:6)
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddressVC.dismissKeyboard))
//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
//tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}