How to force a UIViewController to Portrait orientation in iOS 6

后端 未结 16 1619
别那么骄傲
别那么骄傲 2020-11-22 06:03

As the ShouldAutorotateToInterfaceOrientation is deprecated in iOS 6 and I used that to force a particular view to portrait only, what is the c

16条回答
  •  天命终不由人
    2020-11-22 07:01

    I have a relatively complex universal app using UISplitViewController and UISegmentedController, and have a few views that must be presented in Landscape using presentViewController. Using the methods suggested above, I was able to get iPhone ios 5 & 6 to work acceptably, but for some reason the iPad simply refused to present as Landscape. Finally, I found a simple solution (implemented after hours of reading and trial and error) that works for both devices and ios 5 & 6.

    Step 1) On the controller, specify the required orientation (more or less as noted above)

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    
    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        NSInteger mask = UIInterfaceOrientationMaskLandscape;
        return mask;
    
    }
    

    Step 2) Create a simple UINavigationController subclass and implement the following methods

    -(BOOL)shouldAutorotate {
            return YES;
    }
    - (NSUInteger)supportedInterfaceOrientations {
            return UIInterfaceOrientationMaskLandscape;
    }
    

    Step 3) Present your viewController

    vc = [[MyViewController alloc]init];
    MyLandscapeNavigationController *myNavigationController = [[MyLandscapeNavigationController alloc] initWithRootViewController:vc];
    [self myNavigationController animated:YES completion:nil];
    

    Hope this is helpful to someone.

提交回复
热议问题