This question is strictly about iOS9+
Say you have an ordinary modern app (autolayout, storyboard, universal) which does allow all four rotation positions
you want it to autorotate in the normal way, so it will change to your new constraint-based layouts when user rotates device from landscape to portrait
But you simply want NO animation during the user rotating the device. You want it to just "click" to the new sideways or upright layout.
The only way I have been able to achieve this is by adding:
override func viewWillTransitionToSize(size:CGSize,
withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)
{
coordinator.animateAlongsideTransition(nil, completion:
{_ in
UIView.setAnimationsEnabled(true)
})
UIView.setAnimationsEnabled(false)
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator);
}
to one view controller, a highest or near-highest VC which holds the rest of the container views or whatever in the scene.
This is basically the same ancient idea of using willRotateToInterfaceOrientation/didRotateFromInterfaceOrientation (both now unusable in modern iOS) to turn animations on-off.
However there are many problems
this does not work AppWide, it is a mess, it is scene-based
it seems Very Bad to just turn off all animations
you can see all sorts of racetrack
This question is strictly about iOS9+
These days, Is there any better way to turn off the rotation animations in an app which supports landscape/portrait ???
This question is strictly about iOS9+
You can use Method swizzling.
This means to are going to change calls to "viewWillTransitionToSize" on any view controller in your application to call "genericViewWillTransitionToSize" instead.
This way you do not have to use subclass, or repeated code over your application.
With that sad, you should be very carful with swizzling, with great power comes great responsibility. Put the class in a place that you, or the next programmer after you, will know how to find it, when he will want to return the rotation animations to view controllers.
As far as I know, there is no better way to do it.
Although you can declare a separate class with this method and make all view controllers in your app its subclasses.
When you rotate the device, all view controllers whose views need to change their sizes receive
viewWillTransitionToSize
method invocation.You need to declare this new class once in your app and then change all your view controller declarations from class
MyViewController: UIViewController
toMyViewController: NoRotateAnimationVC
.The provided implementation disables all animations and reenables them after the transition. So if you override this method in only one View Controller, as long as its view will change size as a result of a rotation, it will disable rotation animations everywhere. But if that view controller is not active, animations won't be disabled.