最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift点击其他区域隐藏键盘

Swift crifan 3095浏览 0评论

一个登陆页面中:

点击输入框,显示键盘:

希望点击其他区域,键盘消失

[解决过程]

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点击其他区域隐藏键盘

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.172 seconds, using 22.12MB memory