首页 文章

无法关闭模态视图控制器

提问于
浏览
3

无法关闭模态视图控制器并返回到根视图控制器 . 动画确实显示但仍然弹出当前的视图控制器 .

我正在开发应用程序而不使用故事板,我想解雇当前的模态视图控制器并返回到根视图控制器 .

有没有正确的方法来做到这一点?

我的根控制器是 Navigation Controller 而我的模态视图控制器是 UIViewController . 这是不工作的根本原因吗?

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)
}

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

4 回答

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

    Try this.

  • 0

    如果要关闭呈现为模态的当前视图控制器,则应调用呈现视图控制器的视图控制器并告诉它关闭呈现的视图控制器:

    self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
    

    presentingViewController

    提供此视图控制器的视图控制器 .

  • 2

    要解雇任何viewController,你应该使用

    func handleBack() {
            self.dismiss(animated: true, completion: {})
        }
    
  • 1

    试试这个 :

    _ = self.navigationController?.popToRootViewController(animated: true)
    

相关问题