【问题标题】:changing device orientation to landscape programmatically doesn't work以编程方式将设备方向更改为横向不起作用
【发布时间】:2012-05-27 02:27:41
【问题描述】:

所以我试图通过执行以下操作来强制更改部分代码的设备方向:

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait ||
        [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown){
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
    }

但是方向没有改变。为什么是这样?基本上当前方向是纵向的,我希望它被强制为横向

【问题讨论】:

  • 它工作正常,因为您必须使用attemptRotationToDeviceOrientation 来定位设备。
  • 你的意思是它不工作?也与尝试旋转到设备方向我不能强迫它到一个特定的方向(即:提供我想要的方向)

标签: iphone objective-c ios ipad


【解决方案1】:

如果 Inder Kumar Rathore 的建议有效,那就太好了。但是文档将方向属性描述为只读的,所以如果它有效,我不确定你是否可以依赖它在未来工作(除非 Apple 做了聪明的事情并改变了这一点;强制改变方向,不管如何用户当前拿着他们的设备,这是一个明显的功能需求)。

作为替代方案,插入到 viewDidLoad 中的以下代码将成功(并且有点奇怪)强制定向(假设您已经按照 roronoa zorro 的建议修改了 shouldAutorotateToInterfaceOrientation):

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];
    [view removeFromSuperview];
    [window addSubview:view];
}

很明显,如果用户当前以纵向模式持有他们的设备,则这样做(因此推测您的 shouldAutorotateToInterfaceOrientation 仅设置为横向,如果用户以纵向模式持有他们的设备,此例程会将其转换为横向)。如果您的 shouldAutorotateToInterfaceOirentation 仅设置为纵向,您只需将 UIDeviceOrientationIsPortrait 与 UIDeviceOrientationIsLandscape 交换即可。

出于某种原因,从主窗口中删除视图然后重新添加它会强制它查询 shouldAutorotateToInterfaceOrientation 并正确设置方向。鉴于这不是 Apple 认可的方法,也许人们应该避免使用它,但它对我有用。你的旅费可能会改变。但是这个SO discussion也指的是其他技术。

【讨论】:

    【解决方案2】:

    在 iOS7 上完美运行

    如果你想要肖像,那么:

    [[UIDevice currentDevice] setValue:
                          [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                forKey:@"orientation"];
    

    如果你想要风景:

    [[UIDevice currentDevice] setValue:
                          [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight]
                                forKey:@"orientation"];
    

    【讨论】:

    • 有没有人成功或以其他方式让这种方法获得 App Store 审查的批准?有人建议我不会,因为它是通过 KVO 而不是通过公共 API 修改值。
    【解决方案3】:

    如果设备处于横向模式,则无法强制设备进入纵向模式,反之亦然。您所能做的就是告诉您的应用程序处于特定模式,无论是横向模式还是纵向模式。您可以通过以下方式做到这一点:-

    1. 你应该实现shouldAutorotateToInterfaceOrientation:

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      { 
          // Return YES for supported orientations
          return UIInterfaceOrientationIsLandscape(interfaceOrientation);
      }
      
    2. 编辑 pList。

      在您的 plist 中添加一个键 UIInterfaceOrientation 并根据您的需要将其属性更改为 UIInterfaceOrientationLandscapeLeftUIInterfaceOrientationLandscapeRightUIInterfaceOrientationPortraitUpsideDown..

    【讨论】:

      【解决方案4】:

      编辑

      这可能在 iOS 7 中不起作用,但在以前的 iOS 版本中起作用,因此现在对这个答案投反对票是没有意义的。这个问题是 2 年前提出的,我当时回答了,当时这是一个可行的解决方案。


      [[UIDevice currentDevice] setOrientation: UIDeviceOrientationLandscapeLeft];
      

      【讨论】:

      • @bharathgangupalli 是的,它有效,但不知道为什么我仍然得到 2 票:(
      • @InderKumarRathore setOrientation 在 iOS6 中已弃用
      • @jeff9888 有什么新方法可以做到这一点吗?如果找到请分享。
      猜你喜欢
      • 1970-01-01
      • 2017-11-15
      • 2023-03-09
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2015-12-25
      • 1970-01-01
      相关资源
      最近更新 更多