Rotation methods deprecated, equivalent of 'didRotateFromInterfaceOrientation'?

62,076

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
    }
}
Share:
62,076
strange
Author by

strange

Updated on July 08, 2022

Comments

  • strange
    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 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.

  • ask123
    ask123 almost 10 years
    Will this method call every time we rotate like didRotateFromInterfaceOrientation?. Can you please share me the method you have used. Thank you
  • strange
    strange almost 10 years
    Yes it will. This is what I do: - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoor‌​dinator>)coordinator { [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCo‌​ordinatorContext> context) { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; // do whatever } completion:^(id<UIViewControllerTransitionCoordinatorContext‌​> context) { }]; [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator]; }
  • ask123
    ask123 almost 10 years
    Thank u :)..Will try this
  • Rashmi Ranjan mallick
    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
    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
    DogCoffee about 9 years
    Thanks - i'll add a swift version as an answer to save people some time
  • Jessica
    Jessica about 9 years
    Is there a way to remove the animation?
  • Graham Perks
    Graham Perks over 8 years
    Only 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
    Groot over 8 years
    Isn't the getter also deprecated though. According to the documentation it is.
  • Trip
    Trip about 8 years
    It'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
    DogCoffee about 8 years
    I 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
    Rajneesh071 about 8 years
    it will call when you press power button and minimize the app.
  • Womble
    Womble about 8 years
    Holy 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
    Saty almost 8 years
    @DogCoffee..its not getting called. Has it anything to do with Simulator?
  • DogCoffee
    DogCoffee almost 8 years
    @Saty works in simulator as well - just checked again then. Works as expected.
  • Anton Duzenko
    Anton Duzenko almost 8 years
    There is a missing bracket in the second code snippet
  • Umit Kaya
    Umit Kaya almost 8 years
    even having this "[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; " at the bottom makes difference
  • Andrius Steponavičius
    Andrius Steponavičius over 7 years
    is there a way to do this without subclassing UIVIewController, because we are doing MVVM-C
  • Kartick Vaddadi
    Kartick Vaddadi about 7 years
    Doesn't work for me on iOS 10 — it prints the old orientation, not the new one.
  • Mike Glukhov
    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
    Deepak Sharma about 7 years
    statusbarorientation is deprecated in iOS 9. What else is the option?
  • Deepak Sharma
    Deepak Sharma about 7 years
    statusbarorientation is deprecated in iOS 9. What else is the option?
  • Deepak Sharma
    Deepak Sharma about 7 years
    Does this works on iPad where both vertical and horizontal classes are Regular on full screen?
  • Jeff
    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 into UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation) or UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation). Hope this helps!
  • cyanide
    cyanide over 5 years
    Why do you need animation? Can't you check 'newCollection' straight away?
  • Mike Glukhov
    Mike Glukhov over 5 years
    @cyanide to sync animation with my
  • ADG
    ADG over 5 years
    Your code is not the same as Apple's, you are only testing the vertical size class.
  • NYC Tech Engineer
    NYC Tech Engineer over 5 years
    It's been a while, looks like the content of the link has changed.