获取iPad应用的发布方向

时间:2010-08-04 03:37:14

标签: iphone cocoa-touch ipad ios

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

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

9 个答案:

答案 0 :(得分:36)

这是预期的行为。 Quoth the UIViewController documentation

  

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

换句话说,就设备而言,在启动应用程序时,方向纵向。在application:didFinishLaunchingWithOptions:之后的某个时刻,它会检测到不同的方向并调用您的shouldAutorotateToInterfaceOrientation:方法,然后调用您应该正常处理的other view rotation methods

答案 1 :(得分:19)

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

-(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];

答案 2 :(得分:13)

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

以下是详细说明:http://bynomial.com/blog/?p=25

答案 3 :(得分:4)

正如上面的博客文章中所提到的,有一组用于测试方向的宏。然而,该博客文章提到了UIDeviceOrientationIsPortrait。我喜欢下面的内容,这是一个小小的转折。

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

我所做的观察是你无法在表格视图中调用此代码,而是将其推送到分割视图控制器中嵌入的导航控制器。换句话说,你不能从主视图控制器调用它。假设您维护对父拆分视图控制器的引用,则必须使用splitviewcontroller.interfaceOrientation替换“self.interfaceOrientation”。

答案 4 :(得分:2)

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

UIInterfaceOrientation orientation = [UIApplication sharedApplication] .statusBarOrientation;

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

答案 5 :(得分:1)

所以问题是关于在启动时检查方向。答案很遗憾“你不能”。

AFTER 启动后,您可以正常方式检查方向(正如其他人所描述的那样)。

如果其他人来这里寻找答案,只需停止查看,因为在启动时未设置方向变量(所有视图框架/边界也报告为纵向,即使它们不是)。

答案 6 :(得分:0)

您希望确保在Info.plist中设置正确的密钥以允许您想要的方向:

UISupportedInterfaceOrientations
    UIInterfaceOrientationPortrait
    UIInterfaceOrientationPortraitUpsideDown
    UIInterfaceOrientationLandscapeLeft
    UIInterfaceOrientationLandscapeRight

答案 7 :(得分:0)

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

答案 8 :(得分:0)

你无法弄清楚发射方向是不正确的,这样做确实是后方的痛苦。

这是你需要做的。

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

您只需要以下代码

-(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中启动,第一个旋转回调序列将实际显示启动方向。
如果在UIInterfaceOrientationPortrait中启动,那么我们需要检查第一个旋转的持续时间是否为非零,然后我们知道它是从肖像启动的。

相关问题