界面方向
您可以在didFinishLaunchingWithOptions 被调用之前获得有关方向的良好信息...
- (BOOL)application:(UIApplication *)application
willFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
在这里可以查询 UIScreen 和 UIApplication:
[UIScreen mainScreen].bounds
[UIApplication sharedApplication].statusBarOrientation
这些将根据应用启动时的操作系统状态为您提供正确的宽度、高度和界面方向。 (在任何情况下都最好避免使用viewController.interfaceOrientation,因为它在 iOS8 中已被弃用。)
在您的问题中,您建议 statusBarOrientation 不正确,但它在 iPad mini / 8.3 上对我来说工作正常。
设备方向
如果您对设备方向感兴趣,则需要开始监听设备方向通知。您可以在视图控制器中的应用程序委托中执行此操作 - 无论哪种方式都不会影响您获得信息的速度,因为设备方向轮询需要时间来启动。此外,如果您面朝上/面朝下,您得到的 first 设备方向回调可能是错误的:我认为它只是从界面方向获得提示。
这就是你开始聆听的方式:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(deviceOrientationDidChange:)
name: UIDeviceOrientationDidChangeNotification
object: nil];
如果您在第一个回调之前检查 deviceOrientation,它将未初始化,因此对您没有多大用处。完成后您应该停止聆听,因为此过程会消耗额外的能量。
以下是启动序列中的一些日志,iPad 正面朝上横向放置:
12:44:39.162 [AppDelegate application:willFinishLaunchingWithOptions:]
{1024, 768} UIInterfaceOrientationLandscapeLeft / 0 UIDeviceOrientationUnknown / 0
12:44:39.165 [AppDelegate application:didFinishLaunchingWithOptions:]
{1024, 768} UIInterfaceOrientationLandscapeLeft / 0 UIDeviceOrientationUnknown / 0
12:44:39.179 [ViewController viewDidLoad]
{1024, 768} UIInterfaceOrientationLandscapeLeft / 0 UIDeviceOrientationUnknown / 0
12:44:39.180 [ViewController viewWillAppear:]
{1024, 768} UIInterfaceOrientationLandscapeLeft / 0 UIDeviceOrientationUnknown / 0
12:44:39.186 [ViewController deviceOrientationDidChange:]
{1024, 768} UIInterfaceOrientationLandscapeLeft / 0 UIDeviceOrientationLandscapeRight / 4
12:44:39.204 [ViewController viewDidAppear:]
{1024, 768} UIInterfaceOrientationLandscapeLeft / 0 UIDeviceOrientationLandscapeRight / 4
12:44:39.249 [ViewController deviceOrientationDidChange:]
{1024, 768} UIInterfaceOrientationLandscapeLeft / 0 UIDeviceOrientationFaceUp / 5
来自deviceOrientationDiDChange 的第一个回调发生在启动后约 35 毫秒。如果设备平放,则第一次回调是不正确的——正确的回调发生在大约 20 毫秒之后。这些第一个 deviceOrientation 通知往往发生在第一个 viewController 上的 viewWillAppear 和 viewDidAppear 之间(但这些事件没有关联)。
作为比较,这里有一家初创公司,iPad 正面朝上,纵向放置:
12:44:01.741 [AppDelegate application:willFinishLaunchingWithOptions:]
{768, 1024} UIInterfaceOrientationPortrait / 2 UIDeviceOrientationUnknown / 0
12:44:01.745 [AppDelegate application:didFinishLaunchingWithOptions:]
{768, 1024} UIInterfaceOrientationPortrait / 2 UIDeviceOrientationUnknown / 0
12:44:01.758 [ViewController viewDidLoad]
{768, 1024} UIInterfaceOrientationPortrait / 2 UIDeviceOrientationUnknown / 0
12:44:01.759 [ViewController viewWillAppear:]
{768, 1024} UIInterfaceOrientationPortrait / 2 UIDeviceOrientationUnknown / 0
12:44:01.765 [ViewController deviceOrientationDidChange:]
{768, 1024} UIInterfaceOrientationPortrait / 2 UIDeviceOrientationPortrait / 1
12:44:01.784 [ViewController deviceOrientationDidChange:]
{768, 1024} UIInterfaceOrientationPortrait / 2 UIDeviceOrientationFaceUp / 5
12:44:01.828 [ViewController viewDidAppear:]
{768, 1024} UIInterfaceOrientationPortrait / 2 UIDeviceOrientationFaceUp / 5
日志记录
[UIScreen mainScreen].bounds.size
[UIApplication sharedApplication].statusBarOrientation
[UIDevice currentDevice].orientation
有一些ENUM->NSString 查找