Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap rootViewController with animation?

Im trying to swap to another root view controller with a tab bar; via app delegate, and I want to add transition animation. By default it would only show the view without any animation.

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

That's how I swapped to another rootview controller.

like image 452
Chris Mikkelsen Avatar asked Dec 14 '16 14:12

Chris Mikkelsen


2 Answers

You can use UIView.transition(with: view) to replace the rootViewController of a 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 }) 
like image 97
d.felber Avatar answered Nov 10 '22 00:11

d.felber


Swift 4

Paste function into AppDelegate:

func setRootViewController(_ vc: UIViewController, animated: Bool = true) {     guard animated, let window = self.window else {         self.window?.rootViewController = vc         self.window?.makeKeyAndVisible()         return     }      window.rootViewController = vc     window.makeKeyAndVisible()     UIView.transition(with: window,                       duration: 0.3,                       options: .transitionCrossDissolve,                       animations: nil,                       completion: nil) } 
like image 31
Wiingaard Avatar answered Nov 10 '22 01:11

Wiingaard