无法关闭模态视图 Controller 并返回到 Root View Controller 。动画确实显示但仍然弹出当前 View Controller 。
我在不使用 Storyboard 的情况下开发应用程序,我想关闭当前的模态视图 Controller 并返回到 Root View Controller 。
有什么正确的方法可以做到这一点吗?
我的根 Controller 是导航 Controller ,而我的模态视图 Controller 是UIViewController。这是不工作的根本原因吗?
模态视图 Controller (PlayerViewController)
func handleBack() {
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}
HomeController 中的 FeedCell.swift (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
}
最佳答案
如果你想关闭当前显示为模态的 View Controller ,你应该调用显示 View Controller 的 View Controller 并告诉它关闭显示的 View Controller :
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
The view controller that presented this view controller.
关于ios - 无法关闭模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40715226/