我的 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");
}
}
关于ios - iPad 显示为纵向,但认为它是横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24219616/