无法关闭模态视图控制器并返回到根视图控制器。动画确实显示了,但仍然弹出当前视图控制器。
我正在开发应用程序而不使用故事板,我想关闭当前的模式视图控制器并返回到根视图控制器。
有什么正确的方法可以做到这一点吗?
我的根控制器是导航控制器,而我的模式视图控制器是UIViewController。这是不工作的根本原因吗?
模态视图控制器(PlayerViewController)
func handleBack() {
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
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)
}
Run Code Online (Sandbox Code Playgroud)
应用程序代理
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
}
Run Code Online (Sandbox Code Playgroud)
如果您想关闭当前以模态形式呈现的视图控制器,则应该调用呈现该视图控制器的视图控制器并告诉它关闭所呈现的视图控制器:
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
呈现此视图控制器的视图控制器。
| 归档时间: |
|
| 查看次数: |
3751 次 |
| 最近记录: |