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(_
You can achieve what you want doing the following (tested with Swift 4.1 and Xcode 9.3):
class ViewController: UIViewController {
...
private var dismissViewTap: UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
dismissViewTap = UITapGestureRecognizer(target: self, action: #selector(dismissView))
if let tap = dismissViewTap {
view.addGestureRecognizer(tap)
} // if let
} // viewDidLoad
@objc private func dismissView() {
guard let tap = dismissViewTap else {
return
} // guard
guard helpView.isHidden == false else {
return
} // guard
helpView.isHidden = true
view.removeGestureRecognizer(tap)
} // dismissView
...
} // ViewController
If you want to keep the gesture recognizer (maybe because you open helpView more than once) change dismissView to this version:
...
@objc private func dismissView() {
guard helpView.isHidden == false else {
return
} // guard
helpView.isHidden = true
} // dismissView
...
That's all...!