iPad平面放置时的发射方向

时间:2022-09-11 07:25:51

I have the following code:

我有以下代码:

 -(void)viewWillAppear:(BOOL)animated {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] UIInterface];

    if(orientation==UIDeviceOrientationLandscapeLeft || orientation==UIDeviceOrientationLandscapeRight) {
        NSLog(@"Device Landscape");
    } else {
        NSLog(@"Device Portrait");
    }

 }

This works perfectly for the simulator and on the device but only if the iPad is not flat on a surface, for example if i lay the iPad on my desk and load the view it will say its portrait and so I cant detect the orientation when the view appears.

这适用于模拟器和设备,但仅当iPad在表面上不平坦时,例如,如果我将iPad放在桌面上并加载视图,它会说出它的肖像,所以我无法检测到方向时视图出现。

Any ideas of the best way to detect the interface orientation when the viewWillAppear is called?

有关在调用viewWillAppear时检测界面方向的最佳方法的任何想法?

Thanks

谢谢

3 个解决方案

#1


27  

You should use [self interfaceOrientation] instead of the device orientation.

您应该使用[self interfaceOrientation]而不是设备方向。

#2


8  

[[UIApplication sharedApplication] statusBarOrientation] also works if you are somewhere in the code that does not have access to [self interfaceOrientation]

[[UIApplication sharedApplication] statusBarOrientation]也适用于您无法访问[self interfaceOrientation]的代码中的某个位置

#3


0  

First, do you know about the interfaceOrientation property of UIViewController? It looks like you are trying to duplicate it. If you really want to do that, simply subscribe to device orientation change notifications, check if the new orientation is valid interface orientation (the is a macro for that) and if yes, store the value to some variable (like lastSeenOrientation). Then you can rely on that value wherever you want.

首先,您了解UIViewController的interfaceOrientation属性吗?看起来你正试图复制它。如果你真的想这样做,只需订阅设备方向更改通知,检查新方向是否是有效的界面方向(这是一个宏),如果是,则将值存储到某个变量(如lastSeenOrientation)。然后,您可以随时随地依赖该值。

#1


27  

You should use [self interfaceOrientation] instead of the device orientation.

您应该使用[self interfaceOrientation]而不是设备方向。

#2


8  

[[UIApplication sharedApplication] statusBarOrientation] also works if you are somewhere in the code that does not have access to [self interfaceOrientation]

[[UIApplication sharedApplication] statusBarOrientation]也适用于您无法访问[self interfaceOrientation]的代码中的某个位置

#3


0  

First, do you know about the interfaceOrientation property of UIViewController? It looks like you are trying to duplicate it. If you really want to do that, simply subscribe to device orientation change notifications, check if the new orientation is valid interface orientation (the is a macro for that) and if yes, store the value to some variable (like lastSeenOrientation). Then you can rely on that value wherever you want.

首先,您了解UIViewController的interfaceOrientation属性吗?看起来你正试图复制它。如果你真的想这样做,只需订阅设备方向更改通知,检查新方向是否是有效的界面方向(这是一个宏),如果是,则将值存储到某个变量(如lastSeenOrientation)。然后,您可以随时随地依赖该值。