无法关闭模态视图控制器并返回到根视图控制器。动画确实显示但仍然弹出当前的视图控制器。
我正在开发app而不使用storyboard,我想解雇当前的模态视图控制器并返回到根视图控制器。
有没有正确的方法呢?
我的根控制器是导航控制器虽然我的模态视图控制器是 UIViewController 。这是不工作的根本原因吗?
模态视图控制器(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
}
答案 0 :(得分:2)
如果要关闭呈现为模态的当前视图控制器,则应调用呈现视图控制器的视图控制器并告诉它关闭呈现的视图控制器:
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
提供此视图控制器的视图控制器。
答案 1 :(得分:1)
let layout = UICollectionViewFlowLayout()
let navController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
window?.rootViewController = navController
试试这个。
答案 2 :(得分:0)
要关闭任何viewController,您应该使用
func handleBack() {
self.dismiss(animated: true, completion: {})
}
答案 3 :(得分:0)
试试这个:
_ = self.navigationController?.popToRootViewController(animated: true)