Swift 3.0如何在单击栏外部时关闭搜索栏上的键盘

时间:2017-05-24 23:28:57

标签: swift uisearchbar uisearchbardelegate

如标题所述,我有兴趣在搜索栏外单击时从搜索栏中解除键盘。类似的问题在touchesBegan中使用了self.view.endEditing(true),但我没有运气。我不确定我是否遗漏了其他任何东西,或者我是否正确设置了我的搜索栏。

INNER JOIN

1 个答案:

答案 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)
}