IOS - How to hide a view by touching anywhere outside of it

后端 未结 12 1515
温柔的废话
温柔的废话 2020-12-29 23:23

I\'m new to IOS programming, I\'m displaying a view when a button is clicked, using the following code inside the button method.

 @IBAction func moreButton(_         


        
12条回答
  •  醉梦人生
    2020-12-29 23:50

    Swift 5.1:

    This should help to dismiss the view once touched outside of the view.

     override func touchesBegan(_ touches: Set, with event: UIEvent?)
        {
            let touch = touches.first
            if touch?.view != self.yourView
            { self.dismiss(animated: true, completion: nil) }
        }
    

提交回复
热议问题