Rotate presented view and lock the orientation of presenting view controller

随声附和 提交于 2019-12-21 18:49:30

问题


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;
}


来源:https://stackoverflow.com/questions/45168318/rotate-presented-view-and-lock-the-orientation-of-presenting-view-controller

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