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 of didRotateFromInterfaceOrientation
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 to UIViewControllerTransitionCoordinator
in order to be called when 'transition' to a new size finishes. Thanks.
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];
}
- (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]; }
–
Upanddown [UIDevice currentDevice].orientation
. You can also pipe this into UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)
or UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)
. Hope this helps! –
Sternway 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)
}
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? –
Figured 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)
}
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 👍
let orient = newCollection.verticalSizeClass switch orient { case .compact: print("Lanscape") // Do something default: print("Portrait") // Do something else }
–
Ascites 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
}
}
[[UIApplication sharedApplication] statusBarOrientation]
is deprecated in iOS9 you have to test against UITraitCollection for various devices.
override func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if newCollection.containsTraitsInCollection(UITraitCollection(verticalSizeClass: .Regular)) {
...
}
}
On the Ipad there is no trait collection change so here is how you detect the rotation from start and completion. Here is the Swift 5 syntax:
override func viewWillTransition(to size: CGSize, with coordinator:
UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { [unowned self] _ in
self.view.backgroundColor = UIColor.blue
print("rotation in progress")
}) { [unowned self] _ in
self.view.backgroundColor = UIColor.green
print("rotation complete")
}
}
© 2022 - 2024 — McMap. All rights reserved.