Where did shouldAutorotateToInterfaceOrientation go in iOS 6?

If you are like me and wondering why your application’s orientation changes are not behaving correctly in iOS 6 but they worked just fine in iOS 5, then you may need to check your shouldAutorotateToInterfaceOrientation method in your view controllers.  It turns out that with the release of iOS 6, Apple thought it would be a good thing to deprecate that method and not really provide any backwards compatibility for the new version to the old methods without code changes.

I ran into this issue while debugging an entirely different issue for an application for a client.  It had taken me a while and also some searching through the tubes to find out why shouldAutorotateToInterfaceOrientation wasn’t being called.  It turns out there are actually two methods that should be used instead for iOS 6; -(BOOL)shouldAutorotate and -(NSInteger)supportedInterfaceOrientations.  If you are still supporting earlier versions of iOS you still need to keep shouldAutorotateToInterfaceOrientation and also include the two new methods.  If you would like to support both and not duplicate any logic you already have created for your application, a simple solution I found was to use the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-(BOOL)shouldAutorotate{
    return YES;
}
-(NSInteger)supportedInterfaceOrientations{
    NSInteger orientationMask = 0;
    if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeLeft])
        orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
    if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight])
        orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
    if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortrait])
        orientationMask |= UIInterfaceOrientationMaskPortrait;
    if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortraitUpsideDown])
        orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
    return orientationMask;
}

Setting the methods up this way means that shouldAutorotate and supportedInterfaceOrientations are only called by iOS 6.  In supportedInterfaceOrientations, you are calling the already existing method shouldAutorotateToInterfaceOrientation passing in the different orientations to determine if you should support that orientation.

There is a lot of information on this subject.  I found a lot of suggestions that did not work for me but this was the best solution I had found.  If you are running into the same issue I hope this helps you figure it out.  If you have a different solution that you have found that worked for you let us know in the comments!!

This entry was posted in Mobile, Technology and tagged , , . Bookmark the permalink.

One Response to Where did shouldAutorotateToInterfaceOrientation go in iOS 6?

  1. Vertex Inc says:

    I was just getting ready to release my very first iPad app ever when I noticed that rotation is now broke in IOS 6. Being very new to Apple development, coding still comes hard to me, and the rotation issue had me a bit paniced.

    Thanks so much for making an easy, elegant solution available!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>