获取iPad应用程序的启动方向

在我的iPad应用程序中,我需要运行一些布局代码来根据方向设置正确的布局。 默认情况下,布局是针对横向configuration的,所以在应用程序以纵向模式启动的情况下,我需要采取额外的操作来正确configuration纵向显示的视图。

在我的应用程序-application:didFinishLaunchingWithOptions:方法中,我使用[[UIDevice currentDevice] orientation]检查[[UIDevice currentDevice] orientation] 。 这里的问题是,即使应用程序在横向启动,它总是返回肖像。 有没有办法解决?

这是预期的行为。 讨论UIViewController文档 :

注意:在启动时,应用程序应始终以纵向方式设置其界面。 在application:didFinishLaunchingWithOptions:方法返回后,应用程序使用上述视图控制器旋转机制在显示窗口之前将视图旋转到适当的方向。

换句话说,就设备而言,在应用程序启动时,方向纵向的。 在application:didFinishLaunchingWithOptions:之后的某个时刻application:didFinishLaunchingWithOptions:它将检测到不同的方向,并调用您的shouldAutorotateToInterfaceOrientation:方法,然后调用其他视图旋转方法 ,您应该正常处理这些方法 。

这是检查发射方向的最佳方法。 首先,在AppDelegate中创build一个检查方向的新方法:

 -(void)checkLaunchOrientation:(id)sender{ UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; BOOL isLandscape = UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation); if (UIInterfaceOrientationIsLandscape(orientation) || isLandscape) { //do stuff here } } 

在应用程序结束-application:didFinishLaunchingWithOptions:运行

  [self performSelectorOnMainThread:@selector(checkLaunchOrientation:) withObject:nil waitUntilDone:NO]; 

在你的视图控制器中使用self.interfaceOrientation – 它是由iOS为你设置的UIViewController属性,在某些情况下比[[UIDevice currentDevice] orientation]更可靠。

这里有详细的描述: http : //bynomial.com/blog/?p=25

正如在上面的博客文章中提到的,有一组testing方向的macros。 那篇博文却提到了UIDeviceOrientationIsPortrait。 我喜欢以下内容,这是一个小小的转折。

 if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { NSLog(@"Portrait"); } else { NSLog(@"Landscape"); } 

我所做的一个观察是,您不能在表格视图中调用此代码,并将其推入embedded在分割视图控制器中的导航控制器。 换句话说,你不能从主视图控制器调用它。 您必须用splitviewcontroller.interfaceOrientationreplace“self.interfaceOrientation”,假设您维护对父分割视图控制器的引用。

使用状态栏方向来检测它。

UIInterfaceOrientation orientation = [UIApplication sharedApplication] .statusBarOrientation;

然后在你从上面获得的“方向”上执行if。

所以问题是关于在启动时检查方向。 答案是可悲的“你不能”。

但启动后,您可以按照正常方式(如其他人所述)检查方向。

如果有其他人来这里寻找答案,只需停下来看,因为在启动时,方向variables没有被设置(所有的视图帧/边界也报告是纵向的,即使他们不是)。

你要确保你在你的Info.plist中设置正确的键来允许你想要的方向:

 UISupportedInterfaceOrientations UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight 

不是你需要另一个答案,但我想我应该补充说,你几乎从不想使用[[UIDevice currentDevice] orientation] 。 该方法返回设备的方向,这不一定与接口的方向相同。

不能确定发射的方向,确实是后面的痛苦。

这是你需要做的。

你的第一个UIViewController需要有一些特殊的逻辑来获取你想要的信息。
你甚至可能想为这些目的创build一个UIStartupController,如果这对你的stream程很重要的话。
在我的项目中,我们已经有了这样一个启动控制器。

所有你需要的是下面的代码

 -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.launchOrientation = UIDeviceOrientationUnknown; } return self; } -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; if (self.launchOrientation == UIDeviceOrientationUnknown && duration > 0) self.launchOrientation = UIInterfaceOrientationPortrait; else self.launchOrientation = toInterfaceOrientation; } 

基本上,如果我们不在UIInterfaceOrientationPortrait中启动,第一个循环callback序列实际上会揭示启动方向。
如果在UIInterfaceOrientationPortrait中启动,那么我们需要检查第一轮的持续时间是否为非零,然后我们知道它是从肖像启动的。