ios - 使用侧面菜单更改 Root View Controller

标签 ios swift side-menu

我正在尝试用另一个 ViewController 更改 RootViewController。但我想不通。我面临一些问题

通过上面的代码更改了 rootViewController 之后,新的 viewController 就消失了。 在控制台日志中:不鼓励在分离的 View Controller 上呈现 View Controller 。 请帮助我!

我的代码是:

func changeRootView(){
guard let delegate = UIApplication.shared.delegate else {
    return
}
guard let window = (delegate as! AppDelegate).window else {
    return
}
UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
    let lgv = DriverMainViewController()
    window.rootViewController = UINavigationViewController(rootViewController: lgv)
}, completion: { completed in
    SideMenuManager.menuLeftNavigationController!.dismiss(animated: true, completion: nil)
    print ("changed")
})

Picture before change RootviewController When I clicked that gray button then changeRootView function will run.

Then changeRootView function changed App keyWindow's rootViewController

但是这个蓝色背景的 viewController 在 1 秒后就消失了。 此屏幕截图是在消失后的新 Root View Controller 。

最佳答案

我认为这里发生的事情是,当您设置窗口的 rootViewController 时,旧的 rootViewController 不再被引用,它被 ARC 删除。您可能会 try catch 外出 View Controller ,以便它在动画持续期间保持不变。试试这个:

func changeRootView(){
    guard let delegate = UIApplication.shared.delegate else { return }
    guard let window = (delegate as! AppDelegate).window else { return }

    // capture a reference to the old root controller so it doesn't
    // go away until the animation completes
    let oldRootController = window.rootViewController

    UIView.transition(with: window, 
                  duration: 0.3, 
                   options: .transitionCrossDissolve, 
                animations: {
                    let lgv = DriverMainViewController()
                    window.rootViewController = UINavigationViewController(rootViewController: lgv)
                }, 
                completion: { completed in

                    // OK, we're done with the old root controller now
                    oldRootController = nil

                    SideMenuManager.menuLeftNavigationController!.dismiss(animated: true, completion: nil)
                    print ("changed")
                }
    )
}

这段代码所做的是添加对窗口现有 Root View Controller 的引用,然后在完成 block 中捕获它以控制它存在的时间。

关于ios - 使用侧面菜单更改 Root View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43948621/

相关文章:

ios - 登录后侧边菜单不显示

ios - 当侧面菜单快速关闭时如何更改为原始 View 颜色

html - 如何删除侧面菜单 ionic 2中的下划线

ios - swift prepareForSegue 不工作/exc_breakpoint(代码=exc_i386_bpt 子代码=0x0)

ios - 更新 (15) 后现有部分中包含的行数必须等于

iphone - UIModalTransitionStyleFlipHorizo​​ntal 垂直?

swift - 对关联类型有约束的通用下标导致 "Cannot subscript a value of type..."

ios - 将 Objective-C 枚举转换为 Swift 字符串

ios - realm 移动平台 - realm 可以用作数据库吗?

ios - 无法更新 FBSDKCoreKit POD iOS