Swift viewWillAppear not being called after dismissing view controller

In iOS 13 , if you are presenting a view controller and when you are coming back viewWillAppear doesn't get called . I have changed it from present to push view controller and methods are getting called now .


You need to set the correct presentationStyle. If you want that your presentedController will be fullScreen and call it the previous viewWillAppear, then you can use ".fullScreen"

let viewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginController") as! LoginController

let navigationController: UINavigationController = UINavigationController(rootViewController: viewController)

navigationController.modalPresentationStyle = .fullScreen

present(navigationController, animated: true, completion: nil)

Changing presentation style to .fullScreen works but changes the appearance of the presented view Controller. If u want to avoid that u can override the viewWillDisappear method in the presented viewcontroller and inside it add presentingViewController?.viewWillAppear(true).

Example:

 class ViewControllerA: UIViewController {
 
      override func viewDidLoad() {
          super.viewDidLoad()
          
          //put the presenting action wherever you want

          let vc = ViewControllerB()
          navigationController.present(vc, animated: true)
      }

      override func viewWillAppear(_ animated: Bool) {
           super.viewWillAppear(animated) 
           //refresh Whatever 
      }
 }

 class ViewControllerB: UIViewController {
 
      override func viewDidLoad() {
          super.viewDidLoad()
      }

      override func viewWillDisappear(_ animated: Bool) {
          super.viewWillDisappear(animated)

          //this will call the viewWillAppear method from ViewControllerA  

          presentingViewController?.viewWillAppear(true)
      }
 }

If presented viewController is half screen then you will have to Call the viewWillAppear of presenting viewController manually in side presented view controller's viewWillDisappear. Add following code to your Presented view controller.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    presentingViewController?.viewWillDisappear(true)
}    

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    presentingViewController?.viewWillAppear(true)
}

Note: you must have to call 'presentingViewController?.viewWillDisappear(true)' to get your Presenting view controllers viewWillAppear execute everytime.

Tags:

Ios

Swift