I'm trying to find a method call I can latch onto once a device's orientation has changed. Is there something identical to didRotateFromInterfaceOrientation that isn't deprecated?
As of iOS 8, all UIViewControllers inherit the UIContentContainer protocol, one of whose methods is - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
, which you can (simplistically) override like this:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Stuff you used to do in willRotateToInterfaceOrientation would go here.
// If you don't need anything special, you can set this block to nil.
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Stuff you used to do in didRotateFromInterfaceOrientation would go here.
// If not needed, set to nil.
}];
}
You'll notice there's nothing specific about orientation, which is by design; iOS view rotations are essentially a composition of a specific translation matrix operation (resizing a view from one aspect ratio to another is just a specific case of a general resizing operation where the source and target view sizes are known in advance) and a rotation matrix operation (which is handled by the OS).
Swift 3 version of fullofsquirrels's answer:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { context in
context.viewController(forKey: UITransitionContextViewControllerKey.from)
// Stuff you used to do in willRotateToInterfaceOrientation would go here.
// If you don't need anything special, you can set this block to nil.
}, completion: { context in
// Stuff you used to do in didRotateFromInterfaceOrientation would go here.
// If not needed, set to nil.
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With