ios - iPad 显示为纵向,但认为它是横向

标签 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)

最佳答案

问题是您将根据 UIDeviceOrientation 枚举将设备方向发送到需要 UIInterfaceOrientation 值的函数。

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

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

如果您查看两种方向类型的枚举声明(下面的文档链接),您会发现由于设备方向包含“无”值而导致值不对齐。无论如何,更改您的代码以使用 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

关于ios - iPad 显示为纵向,但认为它是横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24219616/

相关文章:

ios - 无法在 tableViewCell 中放置图像

ios - 如何生成所有可能的组合?

ios - Xamarin iOS ModernHttpClient Owin OAuth2 Bearer Token Authentication DefaultHeader 导致 "System.Net.WebException: cannot parse response"

iphone - 排除不支持多任务处理的设备

c++ - 计算 0 和 360 之间的两个 vector 之间的角度始终在同一方向

javascript - 三个 JS 万向节锁在旋转

c++ - 魔法粒子

ios - 隐藏状态栏不起作用的iOS

iphone - 图标尺寸不符合尺寸要求 (0x0)

Java/安卓 : Continuously rotate ImageView image on tap of left/right side of screen