Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotation behaving differently on iOS6

I did an App which is tab-based. Nothing needs to be on landscape mode but a couple of views. It worked OK on iOS5 and I was pretty happy with the result. However with iOS6 and without messing with anything, it now rotates all the views and the consequences are not nice.

Because its a tab-based app, the couple of view I need in landscape are modalViews. That way I didn't mess with the tabbar and I had only to chose portrait in the "Supported Orientations" setting on build options and set:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

on the views I wanted landscape.

Now with iOS6 this views are also on portrait mode, no matter what and they do not show the landscape mode even if I rotate the device. Likewise, if I allow all the orientations on the "Supported orientations", they all rotate, no matter what I put on the method above.

On all the views I haven't check the box "Use Autolayout" on storyboards.

Any help here?

*EDIT** Now that I see it, the App I have on the device works fine. I've installed with a promo code, not from Xcode, only to see whether my customers are having problems or not. Fortunatelly they are not. The problem remains though.

like image 611
Marcal Avatar asked Sep 21 '12 19:09

Marcal


3 Answers

The most important part of the documentation I found for this issue is:

When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window

To make my app fully working for autorotation in iOS 6, I had to do the following:

1) I created a new subclass of UINavigationController, and added shouldAutorotate and supportedInterfaceOrientation methods:

// MyNavigationController.h:
#import <UIKit/UIKit.h>

@interface MyNavigationController : UINavigationController

@end

// MyNavigationController.m:
#import "MyNavigationController.h"
@implementation MyNavigationController
...
- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}
...
@end

2) In the AppDelegate, I did use my new subclass to show my root ViewController (it is introScreenViewController, a UIViewController subclass) and did set the self.window.rootViewController, so it looks that:

    nvc = [[MyNavigationController alloc] initWithRootViewController:introScreenViewController];
    nvc.navigationBarHidden = YES;
    self.window.rootViewController = nvc;
    [window addSubview:nvc.view];
    [window makeKeyAndVisible];
like image 123
iMathieuB Avatar answered Nov 15 '22 19:11

iMathieuB


This is the alternative solution for iOS6 in case you are using the tab bar controller. It also shows that is NOT needed to override UINavigationController or even UITabBarController.

In your xyzAppDelegate.h add this interface:

@interface UITabBarController (MyApp)
@end

And in xyzAppDelegate.m add these methods:

@implementation UITabBarController (MyApp) 

-(BOOL)shouldAutorotate
{
  return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
  // your custom logic for rotation of selected tab
  if (self.selectedIndex==...) {
    return UIInterfaceOrientationMaskAll;
  } 
  else {
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
  }
}

@end

Also, set the root view controller for the app window:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  ...
  [self.window setRootViewController:tabBarController];
like image 37
Krešimir Prcela Avatar answered Nov 15 '22 17:11

Krešimir Prcela


Are you setting the rootViewController in the delegate? For example,

    self.window.rootViewController = self.navigationController;

When I was doing some iOS6 testing it wouldn't work properly until I did that...

like image 23
Mike M Avatar answered Nov 15 '22 17:11

Mike M