将视图方向设置为纵向将被忽略

时间:2015-06-08 14:33:20

标签: ios objective-c iphone ipad uiinterfaceorientation

我尝试根据设备是在iPhone还是iPad上运行来设置视图的方向。我有一个supportedInterfaceOrientations方法,我从viewDidLoad调用:

[self supportedInterfaceOrientations];

supportedInterfaceOrientations方法中,我检查设备是否为iPhone。如果是,则视图应仅设置为纵向。否则,应支持所有方向:

- (NSUInteger)supportedInterfaceOrientations
{
    //If the device is an iPhone, we're going to make this page portrait-only
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskPortrait;

    }

    //If it's an iPad, we'll support all orientations.
    else {
        return  UIInterfaceOrientationMaskAll;
    }
}

但是,当我在iPhone上运行应用程序时,视图不会在纵向模式下保持锁定状态,而是会根据手机的方向进行旋转。有没有人知道我做错了什么?谢谢!

编辑我忘了提到这个特殊视图是导航控制器的一部分,我刚刚意识到这可能是导致问题的原因。

EDIT2 此特定视图的xib也设置为' portrait'在'模拟指标'部分。另外,下面是构建视图控制器并显示视图的代码:

self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
self.navigationController.delegate = self;
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

1 个答案:

答案 0 :(得分:0)

使用:

- (BOOL)shouldAutorotate {
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    //If the device is an iPhone, we're going to make this page portrait-only
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskPortrait;
    }
    //If it's an iPad, we'll support all orientations.
    else {
        return  UIInterfaceOrientationMaskAll;
    }
}