【问题标题】:Get launch orientation of iPad app获取 iPad 应用程序的启动方向
【发布时间】:2011-03-25 01:30:53
【问题描述】:

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

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

【问题讨论】:

    标签: iphone cocoa-touch ipad ios


    【解决方案1】:

    这是预期的行为。阔思the UIViewController documentation

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

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

    【讨论】:

    【解决方案2】:

    这是在启动时检查方向的最佳方式。首先,在 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];
    

    【讨论】:

    • 很好的解决方案!从 inside -application:didFinishLaunchingWithOptions: 检查 [[UIDevice currentDevice] orientation] 返回未知方向,但从使用 performSelectorOnMainThread:withOptions:waitUntilDone: 运行的方法中检查效果很好。
    【解决方案3】:

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

    这里有详细说明:http://bynomial.com/blog/?p=25

    【讨论】:

    • 实际上有三个方向属性:UIDevice.orientationUIViewController.interfaceOrientationUIApplication.statusBarOrientation
    • 简单且结果非常一致。
    【解决方案4】:

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

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

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

    【讨论】:

      【解决方案5】:

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

      UIInterfaceOrientation 方向 = [UIApplication sharedApplication].statusBarOrientation;

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

      【讨论】:

      • 注意:InterfaceOrientation(= 状态栏方向)与设备方向相同!因此它不适合检测设备检测。
      • 总之,它给了我比 UIDevice 类及其方法更一致和准确的定位结果。
      【解决方案6】:

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

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

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

      【讨论】:

        【解决方案7】:

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

        UISupportedInterfaceOrientations
            UIInterfaceOrientationPortrait
            UIInterfaceOrientationPortraitUpsideDown
            UIInterfaceOrientationLandscapeLeft
            UIInterfaceOrientationLandscapeRight
        

        【讨论】:

        • 我的 plist 文件中有所有 4 个方向。
        【解决方案8】:

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

        【讨论】:

          【解决方案9】:

          你不能弄清楚发射方向,这是真的,这样做很痛苦。

          这是你需要做的。

          你的第一个 UIViewController 需要有一些特殊的逻辑来获取你想要的信息。
          如果 UIStartupController 对您的流程非常重要,您甚至可能只想为这些目的创建一个 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 中启动,那么我们需要检查第一个旋转的持续时间是否不为零,然后我们知道它是从纵向启动的。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-11-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-06
            • 1970-01-01
            相关资源
            最近更新 更多