【问题标题】:Xcode IOS Lock Some orientation to Portrait and Some to landscapeXcode IOS将某些方向锁定为纵向,而将某些方向锁定为横向
【发布时间】:2014-12-01 20:32:05
【问题描述】:

我正在开发一个有大约 12 个视图的应用程序。我希望一些是纵向的,而另一些是横向的。

如何将某些视图的方向锁定为纵向而将其他视图的方向锁定为横向?

我尝试了前面示例中的以下代码,但它没有锁定方向。

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

【问题讨论】:

    标签: ios xcode orientation portrait


    【解决方案1】:

    经过将近 24 小时的尝试,我终于解决了这个问题。对于一个“ShouldAutoRotate”永远不会被调用。这就是我到处都能找到的“只需添加 shouldAutoRotate”......没用。我也不确定为什么人们对我的问题投反对票。

    对我有用的是将以下方法添加到我的 Appdelegate.m 文件中。

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    
        NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
    
        if(self.window.rootViewController){
            UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
            orientations = [presentedViewController supportedInterfaceOrientations];
        }
    
        return orientations;
    }
    

    然后在viewcontroller.m文件中添加下面的方法我要锁定到横向。

    - (NSUInteger)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskLandscape;
    }
    

    【讨论】:

    • 如果您在构建设置中启用其他界面方向,shouldAutoRotate 应该可以工作。
    【解决方案2】:

    您必须在 ViewController 中实现 supportedInterfaceOrientationshouldAutorotate

    Apple Documentation

    【讨论】:

    • 嗨 LucaD,添加 -(NSUInteger)supportedInterfaceOrientations 和或 -(BOOL) shouldAutorotate。视图仍在旋转。取自此处的示例chrisrisner.com/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多