Rotation methods deprecated, equivalent of 'didRotateFromInterfaceOrientation'?
Solution 1
Okay found it, just have to use the animateAlongsideTransition:completion:
method on the passed UIViewControllerTransitionCoordinator
.
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
// do whatever
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
Solution 2
The Swift Version of the answer by strange
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in
let orient = UIApplication.sharedApplication().statusBarOrientation
switch orient {
case .Portrait:
println("Portrait")
// Do something
default:
println("Anything But Portrait")
// Do something else
}
}, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
println("rotation completed")
})
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}
Solution 3
iOS 10.3 & Swift 3
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { (_) in
let orient = newCollection.verticalSizeClass
switch orient {
case .compact:
print("Lanscape")///Excluding iPads!!!
default:
print("Portrait")
}
}, completion: { _ in
print("rotation completed")
})
super.willTransition(to: newCollection, with: coordinator)
}
Solution 4
The accepted answer in Swift 3:
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { (_) in
let orient = UIApplication.shared.statusBarOrientation
switch orient {
case .portrait:
print("Portrait")
// Do something
default:
print("Anything But Portrait")
// Do something else
}
}, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
print("rotation completed")
})
super.willTransition(to: newCollection, with: coordinator)
}
It works fine for me 👍
Solution 5
Since the question was: what was the equivalent of didRotateFromInterfaceOrientation
I thought I'd contribute the code below:
@implementation ViewController
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
if (previousTraitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular) {
NSLog(@"User has rotated to landscape");
} else if (previousTraitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
NSLog(@"User has rotated to portrait");
}
}
@end
I was testing on an iPhone in the simulator, but my print statements won't get run if I test using the iPad since the traitsCollection won't change.
This is strange because this is exactly what Apple recommends:
- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
[super traitCollectionDidChange: previousTraitCollection];
if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass)
|| self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) {
// your custom implementation here
}
}
strange
Updated on July 08, 2022Comments
-
strange almost 2 years
I'm trying to implement the new
viewWillTransitionToSize
method which has been introduced in iOS 8 (all other rotation methods have been deprecated). I'd like to know what the equivalent ofdidRotateFromInterfaceOrientation
is now as there are a number of clean up tasks we need to perform and I can't see a block that we can assign toUIViewControllerTransitionCoordinator
in order to be called when 'transition' to a new size finishes. Thanks. -
ask123 almost 10 yearsWill this method call every time we rotate like didRotateFromInterfaceOrientation?. Can you please share me the method you have used. Thank you
-
strange almost 10 yearsYes it will. This is what I do:
- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; // do whatever } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { }]; [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator]; }
-
ask123 almost 10 yearsThank u :)..Will try this
-
Rashmi Ranjan mallick over 9 years@strange: Thats great. It would be nice if you could add the code snippet in the answer itself. It looks congested in the comment
-
Rashmi Ranjan mallick over 9 years@strange: I have another problem in iOS 8. I have a universal iOS application. This should support all the orientations in iPads and only portrait (home button down) in iPhones. But we need one particular view controller to be locked in landscape mode only in both iPads and iPhones. And once you navigate out of that view controller, it should again support the old orientations. How to achieve this? Please help.
-
DogCoffee about 9 yearsThanks - i'll add a swift version as an answer to save people some time
-
Jessica about 9 yearsIs there a way to remove the animation?
-
Graham Perks over 8 yearsOnly the setter was deprecated. An Apple employee posted, "Reading the status bar orientation is not deprecated, only writing to it is. This may have been an error in how we constructed the header if you are seeing this on the getter." (forums.developer.apple.com/thread/12937)
-
Groot over 8 yearsIsn't the getter also deprecated though. According to the documentation it is.
-
Trip about 8 yearsIt's strange because I put this in my ViewController, and it didn't
print
anything to my log. I guess because this method is not being called. Can you think of anything else that would need to be plugged in order for this to work? -
DogCoffee about 8 yearsI just added it to a empty project (Universal App) and it works fine without the need to add anything. Maybe put a log statement at the very beginning of the method and see if its being called. I cannot think of anything that you would need to add. What iOS you running?
-
Rajneesh071 about 8 yearsit will call when you press power button and minimize the app.
-
Womble about 8 yearsHoly hell, Apple make it hard, don't they? If it wasn't for Stack Overflow contributors such as yourself, I doubt anyone would make anything of substantial worth with the iOS APIs. It's an absolute mess at this point.
-
Saty almost 8 years@DogCoffee..its not getting called. Has it anything to do with Simulator?
-
DogCoffee almost 8 years@Saty works in simulator as well - just checked again then. Works as expected.
-
Anton Duzenko almost 8 yearsThere is a missing bracket in the second code snippet
-
Umit Kaya almost 8 yearseven having this "[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; " at the bottom makes difference
-
Andrius Steponavičius over 7 yearsis there a way to do this without subclassing UIVIewController, because we are doing MVVM-C
-
Kartick Vaddadi about 7 yearsDoesn't work for me on iOS 10 — it prints the old orientation, not the new one.
-
Mike Glukhov about 7 years@VaddadiKartick because you should use
let orient = newCollection.verticalSizeClass switch orient { case .compact: print("Lanscape") // Do something default: print("Portrait") // Do something else }
-
Deepak Sharma about 7 yearsstatusbarorientation is deprecated in iOS 9. What else is the option?
-
Deepak Sharma about 7 yearsstatusbarorientation is deprecated in iOS 9. What else is the option?
-
Deepak Sharma about 7 yearsDoes this works on iPad where both vertical and horizontal classes are Regular on full screen?
-
Jeff over 6 years@DeepakSharma I'm a little late to the party, but you can use
[UIDevice currentDevice].orientation
. You can also pipe this intoUIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)
orUIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)
. Hope this helps! -
cyanide over 5 yearsWhy do you need animation? Can't you check 'newCollection' straight away?
-
Mike Glukhov over 5 years@cyanide to sync animation with my
-
ADG over 5 yearsYour code is not the same as Apple's, you are only testing the vertical size class.
-
NYC Tech Engineer over 5 yearsIt's been a while, looks like the content of the link has changed.