无法关闭模态视图控制器并返回到根视图控制器。动画确实显示,但仍会弹出当前视图控制器。
我正在开发应用程序,不使用storyboard,并且希望关闭当前模态视图控制器并返回到根视图控制器。是否有适当的方法可以做到这一点?
我的根控制器是导航控制器,而我的模态视图控制器是UIViewController。这是不起作用的根本原因吗?
模态视图控制器(PlayerViewController)
"Tapped" 一词显示,但是解除操作无效。
我正在开发应用程序,不使用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
}
"Tapped" 一词显示,但是解除操作无效。
![enter image description here](https://istack.dev59.com/IeHIu.webp)