Sometimes during the lifecycle of an application you may need to change the rootViewController  of your main UIWindow ; a typical case maybe the transition between the initial on-boarding and the home of the app (ie. a UITabBarController ).

In order to handle this edge case you may want to create a top controller (typically an UINavigationController  with invisible navigation bar) which enable you to push your new container using a standard push animation.While basically it works fine, the addition of a container used only to handle this single operation is a bit awful to see.

A way better solution is to apply an animated transition (push/pop or slide) to the rootViewController  set so you will get an animate switch from the current view controller to the new one.

In fact, even if not exposed, you can and its really simple to accomplish via CoreAnimation and CATransition .

The following code implements a new function as extension of UIWindow  class; it takes two arguments: the destination controller and options.
Even the implementation is really simple, check it out.

As you may imagine the destination controller is the controller you want to set as new rootViewController, while options is struct used to group some typical CoreAnimation settings: the animation direction (pop/push/slide from top or bottom), the animation curve (linear, ease in/out), the duration of the animation (by default is 0.25s) and an optional UIView instance used to fade in/out between the old and new view.

You can use it simply by calling the setRootViewController  function.
The following code set newViewController  as the new main UIWindow’s rootViewController  using a push animation as if it were inside a navigation controller.

If not specified Options  struct has:

  • direction: .toRight
  • duration: 0.25s
  • style/curve: .linear
  • background: nil

The following video shows all available animations (you can find it into the project’s example directory on GitHub):

While a CocoaPods lib is really overkilling for a short piece of code like this, I’ve packaged it in a project on Github along with a small example app.

▶︎ UIWindowTransitions on GitHub