栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Swift触摸任意位置以关闭iOS键盘

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Swift触摸任意位置以关闭iOS键盘

override func viewDidLoad() {    super.viewDidLoad()    //Looks for single or multiple taps.     let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")    //Uncomment the line below if you want the tap not not interfere and cancel other interactions.    //tap.cancelsTouchesInView = false    view.addGestureRecognizer(tap)}//Calls this function when the tap is recognized.@objc func dismissKeyboard() {    //Causes the view (or one of its embedded text fields) to resign the first responder status.    view.endEditing(true)}

如果要多次使用此功能,这是另一种执行此任务的方法

UIViewControllers

// Put this piece of pre anywhere you likeextension UIViewController {    func hideKeyboardWhenTappedAround() {        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))        tap.cancelsTouchesInView = false         view.addGestureRecognizer(tap)    }    @objc func dismissKeyboard() {        view.endEditing(true)    }}

现在在每个中

UIViewController
,您所需要做的就是调用此函数:

override func viewDidLoad() {    super.viewDidLoad()    self.hideKeyboardWhenTappedAround() }

此功能作为标准功能包含在我的存储库中,其中包含许多类似这样的有用的Swift扩展,请查看:https
:
//github.com/goktugyil/EZSwiftExtensions



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436412.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号