繁体   English   中英

获取iPad应用的发布方向

[英]Get launch orientation of iPad app

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

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

这是预期的行为。 参阅UIViewController文档

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

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

这是检查发布方向的最佳方法。 首先,在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: 结束-application:didFinishLaunchingWithOptions:运行

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

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

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

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

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

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

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

UIInterfaceOrientation orientation = [UIApplication sharedApplication] .statusBarOrientation;

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

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

但启动 ,你可以检查方向正常方式(如其他人所描述的)。

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

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

这是你需要做的。

你的第一个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中启动,那么我们需要检查第一个旋转的持续时间是否为非零,然后我们知道它是从肖像启动的。

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

UISupportedInterfaceOrientations
    UIInterfaceOrientationPortrait
    UIInterfaceOrientationPortraitUpsideDown
    UIInterfaceOrientationLandscapeLeft
    UIInterfaceOrientationLandscapeRight

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM