Xcode IOS锁定纵向的一些方向和横向的一些方向

时间:2014-12-01 20:32:05

标签: ios xcode orientation portrait

我正在处理一个包含大约12个视图的应用程序。我希望有些人只是肖像,有些人则是风景。

我如何将一些视图的方向锁定为肖像,将其他方向锁定为风景图?

我尝试了上一个示例中的代码,但它没有锁定方向。

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

2 个答案:

答案 0 :(得分:3)

经过近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;
}

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

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

答案 1 :(得分:-1)

您必须在ViewController中实现supportedInterfaceOrientationshouldAutorotate

Apple Documentation

相关问题