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(_ sender: Any)
{
self.helpView.isHidden = false
}
initially, the self.helpView.isHidden
is set to true in viewDidLoad
method to hide the view. Now, how can i dismiss this view by touching anywhere outside the view. From the research, i found that, it can be done by creating a transparent button that fits the whole viewController. So then by clicking on the button, we can make the view to dismiss. Can anyone give me the code in swift 3 to create such button.
Or, if there is any other better way to hide a view, it is welcomed.
I'm using Xcode 8.2, swift 3.0
Thanks in advance.
You can use this method in swift 4.
In Swift 4
In touch began you should write like
Inside the moreButton selected, you can do something like this
You can achieve what you want doing the following (tested with Swift 4.1 and Xcode 9.3):
If you want to keep the gesture recognizer (maybe because you open helpView more than once) change dismissView to this version:
That's all...!
You can use
touchesBegan
method for that: