首页 > 其他 > 详细

点击任意位置隐藏键盘

时间:2020-03-17 23:03:12      阅读:78      评论:0      收藏:0      [点我收藏+]

给view加一个点击事件,点击时收起键盘:

    override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        self.view.endEditing(true)
    }

 

open var cancelsTouchesInView: Bool // default is YES. causes touchesCancelled:withEvent: or pressesCancelled:withEvent: to be sent to the view for all touches or presses recognized as part of this gesture immediately before the action method is called.

如果不加tap.cancelsTouchesInView = false这一句,view中其他的点击事件会失效,例如tableview的didSelectRow。

点击任意位置隐藏键盘

原文:https://www.cnblogs.com/argenbarbie/p/12513759.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!