1

I'm having trouble figuring out how to handle device rotation on iOS 6. I have three things that need to change separately when the device is rotated.

  • I have a parent UIViewController that handles multiple sub UIViewControllers or UINavigationControllers (Its basically a custom UITabBarController). I do not want this to rotate.
  • Each of these sub view controllers, will either rotate or not rotate depending on its own settings. (I want some to rotate and some to not).
  • In the tab bar, I want each tab icon (a UIView) to rotate to the orientation.

How would I go about making this happen in iOS 6, I got everything working in iOS 5.

Here is what I have so far:

In the parent UIViewController:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (BOOL)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

In the sub view controllers:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
        return YES;
}

- (BOOL)shouldAutorotate
{
        return YES;
}

- (BOOL)supportedInterfaceOrientations
{
        return UIInterfaceOrientationMaskAll;
}
Brandon Mcq
  • 575
  • 2
  • 10
  • 24
  • Check out my post: http://stackoverflow.com/a/12538622/1575017 – rocky Sep 27 '12 at 01:35
  • I saw your post, which helped. I added what I think is correct, but nothing will rotate. My info.plist file supports all orientations. – Brandon Mcq Sep 27 '12 at 01:51
  • 1
    are you using the simulator or the real device to test it? I sometimes have trouble getting the simulator to rotate the UI on the screen. HTH – Cashew Sep 27 '12 at 02:21
  • Simulator. That would probably explain why it is not rotating. – Brandon Mcq Sep 27 '12 at 02:32
  • OK, I fixed my simulator, it now responds to interface orientations. I also figured out why the is not working. On iOS 6 should autorotate is only called on the root view controller, and it will only rotate to orientations it supports. (Ignoring all of the sub-views responses) So, how can I override this behavior, and allow the subViews to rotate. (Mainly I just want the tab bar tacked along the edge with the home button. The icons inside rotate with the orientation changes.) – Brandon Mcq Sep 27 '12 at 12:32
  • supportedInterfaceOrientations doesn't return BOOL but NSUInteger – Frans Oct 06 '12 at 12:28

2 Answers2

1

There's a bit more to this to support iOS6 correctly. The iOS 6 Release Notes sketch things out:

https://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html

This bit might be useful:

For compatibility, view controllers that still implement the shouldAutorotateToInterfaceOrientation: method do not get the new autorotation behaviors. (In other words, they do not fall back to using the app, app delegate, or Info.plist file to determine the supported orientations.) Instead, the shouldAutorotateToInterfaceOrientation: method is used to synthesize the information that would be returned by the supportedInterfaceOrientations method.

But you should also take a look at Session 236 from WWDC 2012 - The Evolution of View Controllers.

user456584
  • 86,427
  • 15
  • 75
  • 107
Snowcrash
  • 80,579
  • 89
  • 266
  • 376
  • I read that too, but that is not what is happening. I have the depreciated method for iOs 5, but the two new ones are called as well... I just ran a test, and removed the new methods and now everything works. Apparently you have to implement one or the other. – Brandon Mcq Sep 27 '12 at 11:20
  • Yes, it's either the old way or the new way. You either use shouldAutorotateToInterfaceOrientation (which is deprecated) or axe it completely. – Snowcrash Sep 27 '12 at 14:00
  • 2
    That's not how this is working. In iOS 6, if I remove the new iOS 6 orientation methods, the main view and sub views rotate in any direction regardless of what I specify in the iOS 5 methods. If I add the methods above, nothing rotates. – Brandon Mcq Sep 27 '12 at 14:37
0

If you want to support different orientation in navigation stack, you must subclass UINavigationController first and override supportedInterfaceOrientations.

- (NSUInteger)supportedInterfaceOrientations
{
    //I want to support portrait in ABCView at iPhone only.
    //and support all orientation in other views and iPad.

    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
    {
        // find specific view which you want to control.
        if ([[self.viewControllers lastObject] isKindOfClass:[ABCView class]])
        {
            return UIInterfaceOrientationMaskPortrait;
        }
    }

    //support all
    return UIInterfaceOrientationMaskAll;
}
Jin
  • 1
  • 1