iosswifttransparencyunwind-segueviewdidappear

When presenting modally over current context, on unwind, viewDidAppear is not getting called


I have an issue with an app I'm developing.

Scenario: I have a mainVC (ViewController) which calls a modally presented secondaryVC. After doing a selection in the secondaryVC, I press a UIButton to go back to the mainVC through an unwind segue.

In the secondaryVC, transition is configured as "Cross Disolve" and Presentation, as "Over Current Context" to get see the previous view as background (background is configured with 50% opacity):

enter image description here

Symptom: in this scenario, when going back through the unwind segue, viewDidAppear never gets called. I need it to be called for further check functions to be executed. It gets called if instead of "Over Current Context", I set presentation as "Full Screen" but in that case, I can't see the previous view as a background.

Question: how can I make viewDidApper to be called keeping some transparency on the secondaryVC over the mainVC?

PS: Sorry if there's anything I've missed when writing this question; it's my first. I've searched through this and other forums and I haven't found an solution (or I haven't identified it).


Solution

  • The reason viewDidAppear is not being called is because the first view controller is never disappearing. If you use 'Over Current Context' then you can still see view controller one behind view controller two (assuming there are transparent sections as you have). So view controller one stays visible, never disappears and therefore when view controller two is shown neither viewWillDisappear or viewDidDisappear are called. Then when you unwind back the second view controller disappears but the fist doesn't appear so viewWillAppear and viewDidAppear aren't called.

    If you use 'Full Screen' then the first view controller does disappear and hence all the functions fire.

    If you need to do something when the second view controller disappears and you go back to the first view controller your could put it in the unwind function.

    EDIT

    This is a trick to perform some code like displaying an alert view controller from an unwind segue:

        DispatchQueue.main.async {
            let ac = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
            self.present(ac, animated: true, completion: nil)
        }
    

    Basically what that has done is shove the required code back onto the main thread and it happens after the unwind segue is complete and you are back in the first view controller.