Sunday, September 23, 2012

iOS 6.0 Orientation

In iOS6,UINavigation Controller does not consult their children to determine whether they should autorotate.In another word iOS6 handle orientation by top-most controller.

The shouldAutorotateToInterfaceOrientation: method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientations and shouldAutorotate methods.

iOS6 has three new method to handle orientation
// what we support
- (NSUInteger)supportedInterfaceOrientations 
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

// should autorotate or not
- (BOOL) shouldAutorotate 
{
    return YES;
}

//initial orientation we want to have
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{
    return UIInterfaceOrientationPortrait;
}
But where should we write those methods?


If you try to make the MasterViewController only has Portrait Orientation and the DetailViewController has Portrait Orientation and Landscape Orientation.

I find some tutorial,but It has a bug.
The bug is when your DetailViewController on Landscape Orientation press back button to MasterViewController will show Landscape Orientation.


Here's my solution.
You need to create a class that has a subclass of UINavigationController and set supportedInterfaceOrientations and shouldAutorotate in that class.

First,set this in your subclass of UINavigationController
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return [[self topViewController] supportedInterfaceOrientations]; 
} 

- (BOOL)shouldAutorotate
{
    return YES;
}
set this on MasterViewController
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 
set this on DetailViewController
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}


Another way - It doesn't need to subclass of UINavigationController
Add a Category to UINavigationController
@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end

Another way - Compatible with the iOS4,5 code
Swap Method Implementations


8 comments:

  1. I tried all methods and none of them worked - my adview rotates to whatever direction it wants and it messes my application view when I come back... iOS 6 is useless piece of crap.

    ReplyDelete
    Replies
    1. Those methods work for me,but I also found some orientation bug in iOS 6. I will upload sample file later.

      Delete
    2. This sample file uses "Add a Category to UINavigationController" way.
      https://www.box.com/s/00f1xh44379vvib8nyh2

      Delete
  2. i need this method for tabbar controller app.sample code

    ReplyDelete
  3. This information will help you identify potential problems with drinking water. The most effective way to keep your water clean and accessible is to invest in a mobile water bottle, and use these steps to ensure that the water you drink is clean.
    شركة تنظيف بالرياض
    شركة تنظيف فلل بالرياض
    شركة تنظيف خزانات بالرياض
    شركة مكافحة حشرات بالرياض
    شركة رش مبيدات بالرياض
    شركة مكافحة الصراصير بالرياض
    شركة مكافحة الفئران بالرياض

    ReplyDelete