ios - 使用一个动画关闭和呈现模态视图 Controller

标签 ios uiviewcontroller presentviewcontroller

问题是我不知道如何关闭和显示只有一个过渡动画的 View Controller 。

我的 Storyboard结构是:

enter image description here 我们可以说 A Controller 是 NavigationController 之后的 Controller ,B 是 Startup 引用,C 是 TabBar ViewController。 BC 都以模态方式呈现,带有交叉溶解 过渡。

当用户(从B)登录到应用程序时,C Controller 会以水平翻转 转换模态呈现。当用户注销(从 C)时,B 以相同的方式显示。 在 A Controller 上,我根据用户是否登录执行直接转至 BC

我的问题是,如果我不从 BC 中关闭以前的 View Controller ,该 Controller 就会泄露。相反,如果我拒绝它,A 会在目标 Controller (BC)出现之前显示。

是否可以仅显示Flip Horizo​​ntal 过渡并跨过A View ?

最佳答案

我对这个问题的解决方案是替换当前的 rootViewController,支持不同的转换:

static func replaceRootViewController(with viewController: UIViewController, transition: UIViewAnimationOptions, completion: (() -> ())? = nil) {        
    if transition == .transitionCrossDissolve {
        let overlayView = UIScreen.main.snapshotView(afterScreenUpdates: false)
        viewController.view.addSubview(overlayView)
        UIApplication.shared.keyWindow?.rootViewController = viewController

        UIView.animate(withDuration: 0.65, delay: 0, options: transition, animations: {
            overlayView.alpha = 0
        }, completion: { finished in
            overlayView.removeFromSuperview()
            if let completion = completion{
                completion()
            }
        })
    } else {
        _ = viewController.view
        UIView.transition(with: UIApplication.shared.keyWindow!, duration: 0.65,options: transition, animations: {
            UIApplication.shared.keyWindow?.rootViewController = viewController
        }){_ in
            if let completion = completion {
                completion()
            }

        }
    }
}

关于ios - 使用一个动画关闭和呈现模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41827981/

相关文章:

ios - 在应用程序终止和设备重启后,PushKit通知未到达

ios - 如何知道 UITableView 何时完成 reloadData?

ios - 如何在我当前的 UIViewController 之上呈现一个 UIViewController?

ios - 无法将类型 'testViewController.Type' 的值转换为预期参数类型 'UIViewController'

ios - UITabBarController 在选项卡索引 1 处显示 VC

iphone - 从 mapView 检索当前用户位置,而 map 不在屏幕上

ios - 当 segue 为模态时 performSegueWithIdentifier 非常慢

ios - 当我在 iOS 6.0 的选项卡栏中打开新选项卡时,我的应用程序崩溃了?

ios - 方法 Swizzling 未正确触发?

ios - 在 Swift 中从 TableViewCell 类切换 View Controller