Reputation: 2095
Unable to dismiss modal view controller and return back to the root view controller. Animation did show but still pop out the current view controller.
I am developing app without using storyboard and i would like to dismiss the current modal view controller and back to root view controller.
is there any proper way to do this ?
My Root Controller is Navigation Controller While my modal View Controller is UIViewController. Is it the root cause for not working?
Modal View Controller (PlayerViewController)
func handleBack() {
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}
FeedCell.swift in HomeController (FeedCell.swift)
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let playerViewController = PlayerViewController()
playerViewController.modalPresentationStyle = .overCurrentContext
self.window?.rootViewController?.present(playerViewController, animated: true, completion: nil)
}
AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
UINavigationBar.appearance().barTintColor = UIColor.rgb(red: 230, green: 32, blue: 31)
// get rid of black bar underneath navbar
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(),for: .default)
application.statusBarStyle = .lightContent
let statusBarBackgroundView = UIView()
statusBarBackgroundView.backgroundColor = UIColor.rgb(red: 194, green: 31, blue: 31)
window?.addSubview(statusBarBackgroundView)
window?.addConstraintsWithFormat(format: "H:|[v0]|", views: statusBarBackgroundView)
window?.addConstraintsWithFormat(format: "V:|[v0(20)]", views: statusBarBackgroundView)
return true
}
"Tapped" did showed but dismiss not working
Upvotes: 3
Views: 3859
Reputation: 89
let layout = UICollectionViewFlowLayout()
let navController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
window?.rootViewController = navController
Try this.
Upvotes: 1
Reputation: 2660
Try this :
_ = self.navigationController?.popToRootViewController(animated: true)
Upvotes: 0
Reputation: 1272
To dismiss any viewController ,you should use
func handleBack() {
self.dismiss(animated: true, completion: {})
}
Upvotes: 0
Reputation: 4803
If you want to dismiss the current view controller that was presented as modal, you should invoke the one that presented the view controller and tell it to dismiss the presented view controller:
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
The view controller that presented this view controller.
Upvotes: 5