一个登陆页面中:
点击输入框,显示键盘:
希望点击其他区域,键盘消失
[解决过程]
1.搜:
swift hit hide keyboard
参考:
Close iOS Keyboard by touching anywhere using Swift – Stack Overflow
在当前的视图中添加:
override func viewDidLoad() { super.viewDidLoad() //Looks for single or multiple taps. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard") self.view.addGestureRecognizer(tap) } //when click the view, end editing mode -> will resign first responsor -> dismiss keyboard func dismissKeyboard() { //Causes the view (or one of its embedded text fields) to resign the first responder status. self.view.endEditing(true) } |
的确实现了:
当已经显示了键盘时:
点击其他任意区域(除了另外一个编辑框),就可以让键盘消失了:
转载请注明:在路上 » [已解决]swift点击其他区域隐藏键盘