IOS - How to hide a view by touching anywhere outs

2019-02-12 08:19发布

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.

标签: ios swift uiview
7条回答
forever°为你锁心
2楼-- · 2019-02-12 08:30

You can use this method in swift 4.

add the tag number to the uiview you want to add action

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
    if touches.first?.view?.tag == 10{
        dismiss(animated: true, completion: nil)
        super.touchesEnded(touches , with: event)
    }
}
查看更多
在下西门庆
3楼-- · 2019-02-12 08:45

In Swift 4

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     let touch = touches.first
     if touch?.view == self.view {
        commentsTxtView.resignFirstResponder()
    }
}
查看更多
Lonely孤独者°
4楼-- · 2019-02-12 08:49

In touch began 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
    }
}
查看更多
Deceive 欺骗
5楼-- · 2019-02-12 08:49

Inside the moreButton selected, you can do something like this

 @IBAction func moreButton(_ sender: Any)
    {
        self.helpView.isHidden = false
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissView))
        view.addGestureRecognizer(tap)
    }

    func dismissView() {
        self.helpView.isHidden = true
        self.view.removeGestureRecognizer(tap)
    }
查看更多
虎瘦雄心在
6楼-- · 2019-02-12 08:49

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...!

查看更多
地球回转人心会变
7楼-- · 2019-02-12 08:56

You can use touchesBegan method for that:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    self.helpView.isHidden = true
}
查看更多
登录 后发表回答