aznelite89 aznelite89 - 4 months ago 53
Swift Question

Unable to dismiss the modal view controller

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)


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

window = UIWindow(frame: UIScreen.main.bounds)

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?.addConstraintsWithFormat(format: "H:|[v0]|", views: statusBarBackgroundView)
window?.addConstraintsWithFormat(format: "V:|[v0(20)]", views: statusBarBackgroundView)

return true

"Tapped" did showed but dismiss not working
enter image description here

let layout = UICollectionViewFlowLayout()
let navController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
window?.rootViewController = navController

Try this.