Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS6: supportedInterfaceOrientations not working (is invoked but the interface still rotates)

In my app I have multiple views, some views need to support both portrait and landscape, while other views need to support portrait only. Thus, in the project summary, I have all selected all orientations.

The below code worked to disable landscape mode on a given view controller prior to iOS 6:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

Since shouldAutorotateToInterfaceOrientation was deprecated in iOS6 I've replaced the above with:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMask.Portrait;
}

This method is correctly called when the view appears (I can set a breakpoint to ensure this), but the interface still rotates to landscape mode regardless of the fact that I'm returning the mask for portrait modes only. What am I doing wrong?

It seems that it's currently impossible to build an app that has different orientation requirements per view. It seems to only adhere to the orientations specified in the project summary.

like image 785
aneuryzm Avatar asked Sep 16 '12 14:09

aneuryzm


4 Answers

If your are using a UINavigationController as the root window controller, it will be its shouldAutorotate & supportedInterfaceOrientations which would be called.

Idem if you are using a UITabBarController, and so on.

So the thing to do is to subclass your navigation/tabbar controller and override its shouldAutorotate & supportedInterfaceOrientations methods.

like image 59
Martin Avatar answered Nov 19 '22 21:11

Martin


try change this code in AppDelegate.m

//   self.window.rootViewController = self.navigationController;

    [window setRootViewController:navigationController];

this is the complete answer

shouldAutorotateToInterfaceOrientation not being called in iOS 6

XD

like image 35
NSStudent Avatar answered Nov 19 '22 22:11

NSStudent


In my case I have UINavigationController and my view controller inside. I had to subclass UINavigationController and, in order to support only Portrait, add this method:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

So in the UINavigationController subclass I need to check which orientation is supported by the current topViewController.

- (NSUInteger)supportedInterfaceOrientations
{
    return [[self topViewController] supportedInterfaceOrientations];
}
like image 20
Pavel Avatar answered Nov 19 '22 23:11

Pavel


One thing I've found is if you have an old application that is still doing

[window addSubView:viewcontroller.view];  //This is bad in so may ways but I see it all the time...

You will need to update that to:

[window setRootViewController:viewcontroller]; //since iOS 4

Once you do this the orientation should begin to work again.

like image 15
James Jones Avatar answered Nov 19 '22 22:11

James Jones