我是 IOS 编程的新手,我在单击按钮时显示一个 View ,在按钮方法中使用以下代码。
@IBAction func moreButton(_ sender: Any)
{
self.helpView.isHidden = false
}
最初,self.helpView.isHidden
在 viewDidLoad
方法中设置为 true 以隐藏 View 。现在,我如何通过触摸 View 外的任何地方来关闭此 View 。从研究中,我发现,可以通过创建一个适合整个 viewController 的透明按钮来完成。然后通过点击按钮,我们可以让 View 消失。谁能给我 swift 3 中的代码来创建这样的按钮。
或者,如果有任何其他更好的隐藏 View 的方法,欢迎使用。
我正在使用 Xcode 8.2,swift 3.0
提前致谢。
最佳答案
In touch begins you should write like
override func touchesBegan(_ touches: Set<AnyHashable>, withEvent event: UIEvent) {
var touch: UITouch? = touches.first
//location is relative to the current view
// do something with the touched point
if touch?.view != yourView {
yourView.isHidden = true
}
}
关于IOS - 如何通过触摸 View 之外的任何地方来隐藏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42385450/