【问题标题】:Dismissing to root view controller without showing nested / intermediate view controller在不显示嵌套/中间视图控制器的情况下关闭根视图控制器
【发布时间】:2020-05-14 17:00:42
【问题描述】:

我想关闭所有中间子视图控制器并显示根视图控制器。

以下代码有效: self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)

但是,我可以短暂地看到中间视图控制器在动画消失到根视图控制器之前闪烁。无论如何,它只是直接动画到根视图控制器?

【问题讨论】:

  • 刚刚设置self.view.window?.rootViewController?.dismiss(animated: false, completion: nil)animated: false
  • 它们是在导航控制器中还是以模态方式呈现?如果在导航控制器中,self.navigationController?.popToRootViewController(animated: true)
  • @KishanBhatiya 已经尝试过了,同样的问题。
  • @aledap 不,那是错误的。更改根视图控制器将关闭并清除所有导航堆栈。而呈现根视图控制器不会清除当前的视图控制器,它只会将根视图控制器作为新的视图控制器推送到它们之上。了解其中的区别。
  • 尝试使用let navigationController = UINavigationController(rootViewController: ViewController) let appdelegate = UIApplication.shared.delegate as! AppDelegateappdelegate.window!.rootViewController = navigationController设置rootViewController

标签: ios swift xcode uiviewcontroller uinavigationcontroller


【解决方案1】:

您可以尝试重新设置rootViewController,如下:

let navigationController = UINavigationController(rootViewController: ViewController) 
let appdelegate = UIApplication.shared.delegate as! AppDelegate 
appdelegate.window!.rootViewController = navigationController

【讨论】:

    【解决方案2】:

    你需要打电话

    self.view.window?.rootViewController?.dismiss(animated: false, completion: nil)
    

    如果您不想看到中间控制器,则动画为 false 它仍然闪烁,您可以隐藏其他呈现的视图或使其 alpha 为 0

    喜欢

    if let first = presentedViewController,
            let second = first.presentedViewController,
                let third = second.presentedViewController {
                    second.view.alpha = 0
                    first.view.alpha = 0
                        third.dismiss(animated: false)
    
         }
    

    或者直接设置为rootViewController

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = YourViewController
    

    或在 IOS 13 场景委托中

     let scene = UIApplication.shared.connectedScenes.first
            if let getSceneDelegate : SceneDelegate = (scene?.delegate as? SceneDelegate) {
                getSceneDelegate.window?.rootViewController = YourController
            }
    

    【讨论】:

    • 不,中间视图控制器在根视图控制器出现之前仍然闪烁。唯一的区别是现在根视图控制器只显示没有动画。
    猜你喜欢
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多