iosswiftviewwillappearviewcontroller-lifecyle

Swift viewWillAppear not being called after dismissing view controller


I am presenting a view controller from a view controller called HomeController like so:

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

let navigationController: UINavigationController = UINavigationController(rootViewController: viewController)

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

In the presented view controller LoginController at some point gets dismissed:

self.dismiss(animated: true, completion: nil)

But when it comes back to HomeController it is not calling viewWillAppear, I really need to check on condition on HomeController when it comes back to it, So how can I call viewWillAppear when LoginController dismisses the view?


Solution

  • 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)