我正在尝试查看设备是处于纵向还是横向模式。如果设备没有朝上,我的代码工作得很好。如果它面朝上(和方向== 5),它将无法区分肖像和风景。如果UIDeviceOrientation是FaceUp,那么无论如何都要确定横向/纵向方面的“方向”吗?
我的代码:
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
NSLog(@"orientation: %d", interfaceOrientation);
if (interfaceOrientation == UIDeviceOrientationIsLandscape(interfaceOrientation)) {
NSLog(@"LANDSCAPE!!!");
}
if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) {
NSLog(@"PORTRAIT!!!");
}
答案 0 :(得分:41)
你不应该混淆UIDeviceOrientation
和UIInterfaceOrientation
,它们是不同的但是相关的,如声明所示
typedef enum {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
} UIDeviceOrientation;
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;
UIDeviceOrientation
告诉您设备的方向是什么。 UIInterfaceOrientation
会告诉您界面的方向是什么,并由UIViewController
使用。 UIInterfaceOrientation
显然可以是纵向或横向,而UIDeviceOrientation
可能有不明确的值(UIDeviceOrientationFaceUp
,UIDeviceOrientationFaceDown
,UIDeviceOrientationUnknown
)。
在任何情况下,您都不应尝试使用[[UIDevice currentDevice] orientation]
来确定UIViewController的方向,因为无论设备方向是什么,UIViewController interfaceOrientation
属性都可能不同(例如,如果您的应用不是所有[[UIDevice currentDevice] orientation]
轮播到横向可以是UIDeviceOrientationLandscapeLeft
,而viewController.interfaceOrientation
可以是UIInterfaceOrientationPortrait
。
<强>更新强>
从iOS 8.0开始,不推荐使用[UIViewController interfaceOrientation]
。提供here的替代方案是[[UIApplication sharedApplication] statusBarOrientation]
。这也会返回UIInterfaceOrientation
。
答案 1 :(得分:5)
我制作这个代码骨架来处理想要的&amp;不需要的设备方向,在我的情况下,我想忽略UIDeviceOrientationUnknown,UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown,缓存最后允许的方向。此代码适用于iPhone和iPad设备,对您有用。
- (void)modXibFromRotation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSString *device = [[UIDevice currentDevice]localizedModel];
UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];
if ([device isEqualToString:@"iPad"]) {
if (orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown) {
orientation = (UIDeviceOrientation)cachedOrientation;
}
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
/* Your code */
}
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
/* Your code */
}
}
if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) {
if (orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown) {
orientation = (UIDeviceOrientation)cachedOrientation;
}
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
/* Your code */
}
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
/* Your code */
}
}
}
答案 2 :(得分:0)
从iOS13开始使用
UIInterfaceOrientation orientation;
if (@available(iOS 13.0, *)) {
orientation = self.window.windowScene.interfaceOrientation;
} else {
orientation = [[UIApplication sharedApplication] statusBarOrientation];
}