iOS 6 orientation issues

2019-03-16 21:32发布

问题:

I have an application which normally is a portrait app and only show landscape view for one UIViewController. It works fine until the new iOS 6 is released.

I really don't understand how orientation works in iOS 6. So I wrote a testing app. Here is what I did:

  • Set the orientation of the application to support all orientations.

  • I'm using story board. The rootViewController is embedded in UINavigationController which is in portrait.

  • The code in rootViewController:

    -(NSUInteger)supportedInterfaceOrientations

    {

    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;

}

  • When I clicked the Open bar button, I'll push another (SecondViewController) view controller which supposed to be in landscape mode:

    -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; }

Although this method is called correctly, the second view controller is always also in portrait mode.

Can anybody give me some suggestions? Thanks

回答1:

Here is my solution:

In second view controller's viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIViewController *viewController = [[UIViewController alloc] init];
    [self presentViewController:viewController animated:NO completion:^{
        [viewController dismissViewControllerAnimated:NO completion:nil];
    }];
}

This will force the second view to rotate to landscape orientation which solved my problem. And it works for iOS 5 and 6.



回答2:

For iOS-6, I have done this. It is running fine

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeLeft;}

In second View

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


回答3:

I think that best solution is to stick to official apple documentation. So according to that I use following methods and everything is working very well on iOS 5 and 6. In all of your ViewControllers override following methods.

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

Methods for iOS 6, first method returns supported orientation mask (as their name indicate), you can change it into Landscape or what suites you best.

-(NSInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait; //UIInterfaceOrientationMaskPortrait or LandscapeLeft ... 
}

second one thats tells your VC which is preferred interface orientation when VC is going to be displayed.

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait; //tells your VC in which orientation it should be presented, if you set Porttrait it would be in Portrait or otherwise ...
}

This solution is working smooth, I dont like the idea of creating macros and other stuffs, that goes around this simple solution. Hope this help...