Disable orientation change rotation animation
Based on Nils Munch's Answer, this is the Swift 3 version:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: nil) { (_) in
UIView.setAnimationsEnabled(true)
}
UIView.setAnimationsEnabled(false)
super.viewWillTransition(to: size, with: coordinator)
}
Yes, it is possible to disable the animation, without breaking everything apart.
The following codes will disable the "black box" rotation animation, without messing with other animations or orientation code:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[UIView setAnimationsEnabled:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[UIView setAnimationsEnabled:NO];
/* Your original orientation booleans*/
return TRUE;
}
Place it in your UIViewController and all should be well. Same method can be applied to any undesired animation in iOS.
Best of luck with your project.
For 2016, it appears shouldAutorotateToInterfaceOrientation
is not available to be overridden. The following does seem to work, and cause no other harm.
// these DO SEEM TO WORK, 2016
override func willRotateToInterfaceOrientation(
toInterfaceOrientation:UIInterfaceOrientation, duration:NSTimeInterval)
{
UIView.setAnimationsEnabled(false)
super.willRotateToInterfaceOrientation(toInterfaceOrientation,duration:duration)
}
override func didRotateFromInterfaceOrientation(
fromInterfaceOrientation:UIInterfaceOrientation)
{
super.didRotateFromInterfaceOrientation(fromInterfaceOrientation)
UIView.setAnimationsEnabled(true)
}
However!! Indeed both these functions are deprecated. viewWillTransitionToSize
now takes care of both the "before" and "after".
override func viewWillTransitionToSize(size:CGSize,
withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)
{
coordinator.animateAlongsideTransition(nil, completion:
{_ in
UIView.setAnimationsEnabled(true)
})
UIView.setAnimationsEnabled(false)
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator);
}
In iOS8 you can disable rotation animations by disabling CALayer animations in your view controller's implementation of viewWillTransitionToSize
.
The inverse rotation mentioned in the accepted answer is useful if you want to customize the transition (see WWDC 2014 'View Controller Advancements in iOS 8') but not if you just want to prevent animation.
(N.B. Implementing viewWillTransitionToSize
will prevent the deprecated pre-iOS8 rotation APIs from being called.)
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[CATransaction begin];
[CATransaction setDisableActions:YES];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// You could make a call to update constraints based on the
// new orientation here.
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[CATransaction commit];
}];
}