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

我的故事build筑师devise了一个肖像布局。 当我开始使用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) 

如果您查看两个方向types(下面的文档链接)的枚举声明,则可以看到由于设备方向包含“none”值而导致值不alignment。 无论如何,改变你的代码使用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