Ios 设备接口定向

Ios 设备接口定向,ios,objective-c,uiinterfaceorientation,Ios,Objective C,Uiinterfaceorientation,如何在应用程序启动时获取当前设备界面方向,即包含应用程序图标的主屏幕方向? 例如,iPad在屏幕上(UIDeviceOrientationFaceUp)设置了我想要的横向界面方向(UIInterfaceOrientationAndscapeLeft)。在应用程序启动时,所有界面功能如 viewController.interfaceOrientation [[uiApplication sharedApplication]statusBarOrientation] 给出错误的默认值UIInt

如何在应用程序启动时获取当前设备界面方向,即包含应用程序图标的主屏幕方向? 例如,iPad在屏幕上(
UIDeviceOrientationFaceUp
)设置了我想要的横向界面方向(
UIInterfaceOrientationAndscapeLeft
)。在应用程序启动时,所有界面功能如

  • viewController.interfaceOrientation
  • [[uiApplication sharedApplication]statusBarOrientation]

给出错误的默认值
UIInterfaceOrientationGrait
。存在哪些可能的方式?

接口定向

在调用
didfishLaunchingWithOptions
之前,您可以获得有关方向的良好信息

- (BOOL)application:(UIApplication *)application 
         willFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
在这里,您可以查询UIScreen和UIApplication:

[UIScreen mainScreen].bounds
[UIApplication sharedApplication].statusBarOrientation
这些将根据应用程序启动时的操作系统状态为您提供正确的宽度、高度和界面方向。(在任何情况下,最好避免使用
viewController.interfaceOrientation
,因为它在iOS8中被弃用。)

在你的问题中,你认为StatusBaroOrientation是不正确的,但它在我的iPad mini/8.3上运行良好

设备方向

如果您对设备定向感兴趣,则需要开始侦听设备定向通知。您可以在应用程序中或在视图控制器中委托执行此操作-任何一种方式都不会影响您获得信息的速度,因为设备定向轮询需要时间启动。此外,如果你面朝上/面朝下,你得到的第一个设备方向回调可能是错误的:我认为它只是从接口方向得到了提示

以下是您开始聆听的方式:

[[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
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
deviceOrientationDiDChange
的第一次回调发生在启动后约35毫秒。如果设备是平放的,则第一次回调是不正确的-正确的回调在大约20毫秒后发生。这些第一个设备定向通知往往发生在第一个viewController上的
视图将出现
视图显示
之间(但这些事件不同步)

相比之下,这是一家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
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
lookup

你所说的“App Strartup”是什么意思?你的意思是在
applicationdifinishloadingwithoptions
中吗?试着读一下这篇文章,我认为它回答了你的问题是的,applicationdifinishloadingwithoptions。这篇文章是错的,至少对我来说是错的。正如我在上面所写的,当面向横向的iPad面朝上放置时,[[UIApplication sharedApplication]statusBarOrientation]返回纵向。感谢您如此详细的描述,现在一切都清楚了!