iOS6: supportedInterfaceOrientations not working (

2019-01-06 13:44发布

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.

15条回答
你好瞎i
2楼-- · 2019-01-06 14:24

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

查看更多
Melony?
3楼-- · 2019-01-06 14:24

Try add shouldAutorotate method

查看更多
萌系小妹纸
4楼-- · 2019-01-06 14:24

my solution : subclassed UINavigationController and set it as window.rootViewController

the top viewcontroller of the hierarchy will take control of the orientation , some code examples : subclassed UINavigationController

查看更多
男人必须洒脱
5楼-- · 2019-01-06 14:28

As stated by others if you're using a UINavigationController and you want to customize various views you'll want to subclass the UINavigationController and make sure you have these two components:

@implementation CustomNavigationController

// -------------------------------------------------------------------------------
//  supportedInterfaceOrientations:
//  Overridden to return the supportedInterfaceOrientations of the view controller
//  at the top of the navigation stack.
//  By default, UIViewController (and thus, UINavigationController) always returns
//  UIInterfaceOrientationMaskAllButUpsideDown when the app is run on an iPhone.
// -------------------------------------------------------------------------------
- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations]; 
}

// -------------------------------------------------------------------------------
//  shouldAutorotate
//  Overridden to return the shouldAutorotate value of the view controller
//  at the top of the navigation stack.
//  By default, UIViewController (and thus, UINavigationController) always returns
//  YES when the app is run on an iPhone.
// -------------------------------------------------------------------------------
- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

Then in any view that is a portrait only you would include:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

And in any view that is everything but upside down:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
查看更多
劫难
6楼-- · 2019-01-06 14:28

If you are using UINavigationController, you have to implement shouldAutorotate and supportedInterfaceOrientations in subclass of UINavigationController.

These are able to control by two steps, if shouldAutorotate returns YES then effective supportedInterfaceOrientations. It's a very nice combination.

This example, my mostly views are Portrait except CoverFlowView and PreviewView. The CoverFlowView transfer to PreviewView, PreviewView wants to follow CoverFlowCView's rotation.

@implementation MyNavigationController

-(BOOL)shouldAutorotate
{

if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"PreviewView")])

return NO;

else

return YES;

}



-(NSUInteger)supportedInterfaceOrientations

{

if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"CoverFlowView")])

return UIInterfaceOrientationMaskAllButUpsideDown;

else

return UIInterfaceOrientationMaskPortrait;

}

...

@end
查看更多
▲ chillily
7楼-- · 2019-01-06 14:29

The best way I think is to do a Category rather than subclassing UINavigationController or UITabbarController

your UINavigationController+Rotation.h

#import <UIKit/UIKit.h>

@interface UINavigationController (Rotation)

@end

your UINavigationController+Rotation.m

#import "UINavigationController+Rotation.h"

@implementation UINavigationController (Rotation)

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

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

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


@end

Try to make all your controller import this category and this work like a charm. You can even make a controller not rotating and pushing another controller that will rotate.

查看更多
登录 后发表回答