我试图用标签栏交换到另一个根视图控制器;通过应用程序委托,我想添加过渡动画。默认情况下,它将仅显示视图而没有任何动画。
let tabBar = self.instantiateViewController(storyBoard: "Main", viewControllerID: "MainTabbar") let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.main.bounds) appDelegate.window?.rootViewController = tabBar appDelegate.window?.makeKeyAndVisible()
这就是我换成另一个rootview控制器的方式。
您可以使用UIView.transition(with: view)替换rootViewController的UIWindow:
UIView.transition(with: view)
rootViewController
UIWindow
guard let window = UIApplication.shared.keyWindow else { return } let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: "MainTabbar") // Set the new rootViewController of the window. // Calling "UIView.transition" below will animate the swap. window.rootViewController = vc // A mask of options indicating how you want to perform the animations. let options: UIView.AnimationOptions = .transitionCrossDissolve // The duration of the transition animation, measured in seconds. let duration: TimeInterval = 0.3 // Creates a transition animation. // Though `animations` is optional, the documentation tells us that it must not be nil. ¯\_(ツ)_/¯ UIView.transition(with: window, duration: duration, options: options, animations: {}, completion: { completed in // maybe do something on completion here })