Umair Afzal Umair Afzal - 1 year ago 1379
Swift Question

How to present view controller from right to left in iOS using Swift

I am using presentViewController to present new screen

let dashboardWorkout = DashboardWorkoutViewController()
presentViewController(dashboardWorkout, animated: true, completion: nil)

This presents new screen from top to bottom but I want it to presented from right to left without using

I am using Xib instead of storyboard so how can i do that ?

Answer Source

It doesn't matter if it is xib or storyboard that you are using. Normally, the right to left transition is used when you push a view controller into presentor's UINavigiationController.

Check out this SO answer. The solution is similar to the one @Lion suggested but the animation operates on the self.view.window rather then on the destination view controller's view. Swift version with correct animation:

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.window!.layer.addAnimation(transition, forKey: kCATransition)
presentViewController(dashboardWorkout, animated: false, completion: nil)

Seems like the animated parameter in the presentViewController method doesn't really matter in this case of custom transition. It can be of any value, either true or false.