Xcode IOS 锁定某些方向为纵向,某些方向为横向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27236752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:17:28  来源:igfitidea点击:

Xcode IOS Lock Some orientation to Portrait and Some to landscape

iosxcodeorientationportrait

提问by Mich

I am working on an application that has about 12 views. I want some to be portrait only and others to be landscape.

我正在开发一个有大约 12 个视图的应用程序。我想要一些只是肖像,而另一些则是风景。

How would I go about locking some view's orientation to portrait and others to landscape?

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

I tried the code below from a previous example, but It does not lock the orientation.

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

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

回答by Mich

After almost 24 hours of trying I finally fixed the problem. For one "ShouldAutoRotate" never gets called. And thats all I could find everywhere "Just add shouldAutoRotate"... Didnt work. Also i am not sure why people are down voting my question.

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

What work for me was the add the method below to my Appdelegate.m file.

对我有用的是将下面的方法添加到我的 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;
}

Then add the method below to the viewcontroller.m file I want to be locked to landscape.

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

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

回答by Luca D'Alberti

You have to implement supportedInterfaceOrientationand shouldAutorotatein your ViewController.

您必须在您的 ViewController 中实现supportedInterfaceOrientationshouldAutorotate

Apple Documentation

苹果文档