iPad以肖像显示,但认为它是风景

时间:2014-06-14 11:49:30

标签: ios ipad rotation orientation landscape-portrait

我的Storybuilder采用纵向布局设计。当我启动应用程序并且我的iPad已经转为水平时,它能够正确地检测到它处于水平位置。但是当我用我的iPad处于纵向位置启动应用程序时,它认为它处于横向状态。但是,每次旋转它时,代码都能够正确检测到正确的方向。

- (void) viewDidLoad
{
    [self updateForOrientation];
}

- (void)updateForOrientation
{
    if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) // became portrait
    {
        NSLog(@"is portrait");
        //code for changing layout to portrait position
    }

    else //became horiztontal
    {
        NSLog(@"is horizontal");
        //code for changing layout to horizontal position
    }
}

Output: is horizontal (this is the output whether it starts up as portrait or landscape)

1 个答案:

答案 0 :(得分:1)

问题在于,您正在将UIDeviceOrientation enum方面的设备方向发送到期望UIInterfaceOrientation值的函数。

如果您点击UIInterfaceOrientationIsPortrait(),您可以看到它的定义如下。

#define UIInterfaceOrientationIsPortrait(orientation)  ((orientation) == UIInterfaceOrientationPortrait || (orientation) == UIInterfaceOrientationPortraitUpsideDown)

如果您查看两种方向类型的枚举声明(下面的文档链接),您可以看到由于设备方向包含“none”的值而导致值不对齐。无论如何,更改代码以使用UIInterfaceOrientation应该对此进行排序。例如:

- (void)updateForOrientation
{
    UIInterfaceOrientation currentOrientation = self.interfaceOrientation;

    if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
        NSLog(@"is portrait");
    }else{
        NSLog(@"is horizontal");
    }
}

https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIInterfaceOrientation

https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/c_ref/UIDeviceOrientation