Rotate presented view and lock the orientation of

2019-04-09 13:07发布

问题:

I am working on iPad application which supports only landscape orientation, i want to allow some presented view controller to support all orientation without changing orientation of presenting view controller. Am supporting all orientation in Xcode Settings except upside down.

Code i am using to present view controller

    ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];
    vc.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:vc animated:YES completion:nil];

Code i am using to allowing orientation for presented view controller:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

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

You tube app provide same kind of feature while playing video, any idea how it is working?

Any help will appreciate, thanks.

回答1:

AppDelegate.m

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

if(_isAllModes)
    return UIInterfaceOrientationMaskAll;
else
    return UIInterfaceOrientationMaskPortrait;
}

Your ViewController. Rotation:

[(AppDelegate*)([UIApplication sharedApplication].delegate) setIsAllModes:YES];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];


回答2:

I had a similar issue, but project settings overwrite single ViewController settings you applied programmatically. What I did to solve the issue was leaving the only min acceptable orientation in project settings and extending it in AppDelegate.m like below

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window //newUX
{
    return UIInterfaceOrientationMaskAll;
}

you can do it better anyway, for example, if you want to enable all orientations only in one specific scenario, just saying if last viewController in stack is the "all"-one, like below

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window //newUX
{
    if([self.navController.viewControllers.lastObject isKindOfClass:[MyAllOrientationVC class]])//newUX
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
}