【问题标题】:Rotate presented view and lock the orientation of presenting view controller旋转呈现的视图并锁定呈现视图控制器的方向
【发布时间】:2017-07-18 13:33:11
【问题描述】:

我正在开发仅支持横向的 iPad 应用程序,我希望允许某些呈现的视图控制器支持所有方向而不改变呈现视图控制器的方向。我支持 Xcode 设置中的所有方向,除了颠倒。

我用来呈现视图控制器的代码

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

我用来允许呈现视图控制器方向的代码:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

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

You Tube 应用在播放视频时提供了相同的功能,知道它是如何工作的吗?

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 你解决过这个问题吗?

标签: ios objective-c iphone ipad orientation


【解决方案1】:

AppDelegate.m

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

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

你的视图控制器。旋转:

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

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,但项目设置会覆盖您以编程方式应用的单个 ViewController 设置。我为解决这个问题所做的就是在项目设置中留下唯一可接受的最小方向,并在 AppDelegate.m 中扩展它,如下所示

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

    无论如何,您都可以做得更好,例如,如果您只想在一个特定场景中启用所有方向,只需说明堆栈中的最后一个 viewController 是否为“all”-one,如下所示

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

    【讨论】:

    • 我没有使用导航控制器,如果我改变方向,它会同时改变视图控制器的方向,我只想要呈现的视图控制器方向改变而不是呈现视图控制器 (因为我通过设置呈现样式 Form Sheet 来呈现视图控制器,因此两个视图控制器都可见)
    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    相关资源
    最近更新 更多