Home > Software engineering >  Dismiss the presented view controller after rotataion
Dismiss the presented view controller after rotataion

Time:09-21

on iPad, I have a a ViewController that presented popover on another ViewController.

private lazy var menuPadViewController = MenuViewController()


private func presentMenuVC(from sourceView: UIButton) {
    let nc = UINavigationController(rootViewController: menuPadViewController)
    nc.modalPresentationStyle = .popover
    nc.popoverPresentationController?.sourceView = sourceView
    present(nc, animated: true)
}

Due to UI of the presented MenuViewController, I need to dismiss it when device is rotate, otherwise it would be look so mess.

So, in viewWillTransition I set that the MenuViewController should be dismissed after rotation. It works pretty fine

public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    
    menuPadViewController.dismiss(animated: true)
}

The issue is, it works fine when the MenuViewController is presented (on the display), but when it's already dismissed, menuPadViewController.dismiss(animated: true) will dismiss the parent ViewController. I need to implement some conditions to only dismiss it if it's presented and on the display.

Would be awesome if you can show me the best reliable way to do it, many thanks!

CodePudding user response:

From the presenting view controller, you can check the type of view controller that is currently being presented before dismissing it.

if let presented = presentedViewController,
   presented is YourPresentedViewController {
    // dismiss
}
  • Related