iPad显示肖像,但认为它是风景 [英] iPad shows at portrait but thinks it's landscape
问题描述
我的Storybuilder设计为纵向布局.当我将iPad转到水平位置时启动该应用程序时,它可以正确检测到它处于水平位置.但是,当我将iPad纵向放置时启动该应用程序时,它会认为它是水平的.但是,每次旋转时,代码都能够正确检测到正确的方向.
My Storybuilder is designed with a portrait layout. When I start the app with my iPad already turned to horizontal, it's able to correctly detect it's in a horizontal position. But when I start the app with my iPad in a portrait position, it thinks it's in horizontal. However, every time I rotate it, the code is able to detect the correct orientation properly.
- (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值的函数.
The problem is that you're sending the devices orientation in terms of the UIDeviceOrientation enum to a function that's expecting a UIInterfaceOrientation value.
如果命令单击UIInterfaceOrientationIsPortrait()
,则可以看到它的定义如下.
If you command click on UIInterfaceOrientationIsPortrait()
, you can see that it is defined as follows.
#define UIInterfaceOrientationIsPortrait(orientation) ((orientation) == UIInterfaceOrientationPortrait || (orientation) == UIInterfaceOrientationPortraitUpsideDown)
如果您查看两种方向类型的枚举声明(下面的文档链接),您会发现由于设备方向包含无"值而导致值不对齐.无论如何,将代码更改为使用UIInterfaceOrientation应该可以解决此问题.示例:
And if you look at the enum declarations for the two orientation types (documentation links below), you can see that there is a misalignment in value due to the device orientation containing a value for "none". Anyway, changing your code to use UIInterfaceOrientation should sort this out. Example:
- (void)updateForOrientation
{
UIInterfaceOrientation currentOrientation = self.interfaceOrientation;
if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
NSLog(@"is portrait");
}else{
NSLog(@"is horizontal");
}
}