0

Trying to exit the modal view to go back to the previous scene prior to selecting the modal view. Example attached below:

Select greyed out area

Originally I used some code from github to exit and return to the previous scene via button.

How can I select the greyed out part to exit instead?

Panda
  • 6,955
  • 6
  • 40
  • 55
luke
  • 2,743
  • 4
  • 19
  • 43

1 Answers1

1

Add a UITapGestureRecognizer to the gray view and set it up to a method that dismisses the view controller. E.g:

let tap = UITapGestureRecognizer(target: self, action: "close:")
grayView.addGestureRecognizer(tap)

Put this in your viewDidLoad() for example. Then make the action that responds to the tap, in the global scope of the view controller:

func close(tap: UITapGestureRecognizer) {
    self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}

If you want the view to behave differently, you can try something like this:

func close(tap: UITapGestureRecognizer) {
    let view = tap.view!
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        view.backgroundColor = UIColor.clearColor()
        }) { (success) -> Void in
            self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
    }
}

You´ll maybe have to experiment with the duration of the animation to get it right.

Daniel
  • 40
  • 1
  • 8
  • This works great, but do you have any idea how to not make it slide downwards with the modal view? Right now, the grey view acts as if its a part of the modal view. I want the modal view to slide downwards and then for the greyed out view to disappear separately. – luke Mar 18 '16 at 14:07
  • Added a possible solution to the comment. Please accept the the answer if it resolved your original question. – Daniel Mar 18 '16 at 14:41
  • Can't say it works unfortunately. Tried moving the views around to make it separate as well as the model view inside the grey view but neither seem to work out. btw how do you accept the answer? – luke Mar 18 '16 at 15:25
  • Hmm ok. It´s hard to give an accurate answer to that without seeing your code, or how the page is set up. You can accept an answer by pressing the gray checkmark to the left of an answer :) – Daniel Mar 18 '16 at 15:32
  • Can't say i've done much animation stuff in swift anyway so i'll probably come back to it. Got my main answer so thanks for that anyway :) – luke Mar 18 '16 at 16:34