How do I restrict orientation per view controller in iOS7 in a navigation controller hierarchy

谁说胖子不能爱 提交于 2019-11-29 04:27:48

Simple but it work very fine. IOS 7.1 and 8

AppDelegate.h

@property () BOOL restrictRotation;

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
    return UIInterfaceOrientationMaskPortrait;
else
    return UIInterfaceOrientationMaskAll;
}

ViewController

-(void) restrictRotation:(BOOL) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
}

viewDidLoad

[self restrictRotation:YES]; or NO
marciokoko

Ok, here it is. kinda complicated.

Project Settings must allow P, LL & LR

Storyboard is a UINavController with a UITableViewController with a push bar button segue to a UIViewController.

All scenes in storyboard must have inferred as orientation in simulated metrics. Just saying, cause after a while i had them all with different settings after testing so much.

Must have a Class for NavController, TableViewController and the given UIVController. My app started out as Single view, then I dragged in a UITVC and finally embedded the UITVC in a UINVC. Then I connected the UIVC to the UITVC bar button item I dragged in via push segue.

Set apps window.rootvc to the navVC, your top hierarchy vc (don't forget to set that ID in storyboards or it'll crash of course):

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    UINavigationController *myNavC = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainNav"];
    self.window.rootViewController = myNavC;

Tell big boss UINVC everyone can rotate:

    - (BOOL)shouldAutorotate {
         return self.topViewController.shouldAutorotate;    
    }

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
          return self.topViewController.supportedInterfaceOrientations;    
    }

Restrict tablevc so it won't rotate:

    - (BOOL)shouldAutorotate { return NO; }

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return (UIInterfaceOrientationMaskPortrait); 
    }

Allow last UIVC to rotate:

    - (BOOL)shouldAutorotate { return YES; }

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return (UIInterfaceOrientationMaskAllButUpsideDown);    
    }

Responce

It should not be confused UIDeviceOrientation and interface orientation here is my solution

Appdelegate.h

@property () UIInterfaceOrientationMask restrictRotation;

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
     UIInterfaceOrientationMask restrictionOrientation = 0;

if (_restrictRotation == 0)
    return UIInterfaceOrientationMaskAll;

UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];

switch (currentOrientation)
{
    case UIDeviceOrientationPortrait:

        if (!(UIInterfaceOrientationMaskPortrait & _restrictRotation))
            restrictionOrientation = UIInterfaceOrientationMaskAll;
        else
            restrictionOrientation = UIInterfaceOrientationMaskPortrait;

        break;

    case UIDeviceOrientationPortraitUpsideDown:

        if (!(UIInterfaceOrientationMaskPortraitUpsideDown & _restrictRotation))
            restrictionOrientation = UIInterfaceOrientationMaskAll;
        else
            restrictionOrientation = UIInterfaceOrientationMaskPortrait;

        break;

    case UIDeviceOrientationLandscapeLeft:

        if (!(UIInterfaceOrientationMaskLandscapeLeft & _restrictRotation))
            restrictionOrientation = UIInterfaceOrientationMaskAll;
        else
            restrictionOrientation = UIInterfaceOrientationMaskPortrait;

        break;

    case UIDeviceOrientationLandscapeRight:

        if (!(UIInterfaceOrientationMaskLandscapeRight & _restrictRotation))
            restrictionOrientation = UIInterfaceOrientationMaskAll;
        else
            restrictionOrientation = UIInterfaceOrientationMaskPortrait;

        break;

    case UIDeviceOrientationUnknown:

        if (!(UIInterfaceOrientationUnknown & _restrictRotation))
            restrictionOrientation = UIInterfaceOrientationMaskAll;
        else
            restrictionOrientation = UIInterfaceOrientationMaskPortrait;

        break;

    default:

        NSLog(@"Unknown orientation");

        break;
}

return restrictionOrientation;

}

In each vc add the function

-(void) restrictRotation:(UIInterfaceOrientationMask) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;

}

viewDidAppear

//
// Set vc orientation
//

[self restrictRotation:UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight];
 or what you want.

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:   UIInterfaceOrientationPortrait] forKey:@"orientation"];

or what you want.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!