【问题标题】:Programmatic interface orientation change not working for iOS编程界面方向更改不适用于 iOS
【发布时间】:2014-01-13 20:45:57
【问题描述】:

所以,我有一个项目,我需要在用户按下按钮时强制改变方向。我创建了一个sample app on github 来演示这个问题。

@interface DefaultViewController () {
    UIInterfaceOrientation _preferredOrientation;
}

一些旋转处理位

- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return _preferredOrientation;
}

还有一个开关

- (IBAction)toggleButtonPressed:(id)sender {
    _preferredOrientation = UIInterfaceOrientationIsPortrait(_preferredOrientation)
        ? UIInterfaceOrientationLandscapeRight
        : UIInterfaceOrientationPortrait;

    [self forceOrientationChange];
}


- (void)forceOrientationChange
{
    UIViewController *vc = [[UIViewController alloc] init];
    [self presentViewController:vc animated:NO completion:nil];

    [UIView animateWithDuration:.3 animations:^{
        [vc dismissViewControllerAnimated:NO completion:nil];
    } completion:nil];
}

所以,当您按下切换按钮时,这似乎工作正常,它会按预期改变方向。但是当我开始改变设备的实际方向时,就会出现问题。

重现问题的步骤:

  1. 以纵向打开应用程序
  2. 按下按钮强制将方向更改为横向(使实际设备保持纵向)
  3. 再次按下按钮强制旋转回纵向(仍然保持设备纵向)
  4. 无需按下按钮即可将实际设备旋转为横向

结果是视图不会旋转到横向,但状态栏会。

任何帮助将不胜感激!

【问题讨论】:

    标签: ios objective-c rotation orientation


    【解决方案1】:

    我可以通过在显示和关闭视图控制器之前添加以下行来解决此问题:

    [UIViewController attemptRotationToDeviceOrientation]; 
    

    很难确切说明为什么会这样。此调用通过调用shouldAutorotateToInterfaceOrientation: 和后续旋转方法(如果返回YES)来尝试将interfaceOrientationdeviceOrientation 匹配。

    似乎界面和设备方向不同步,因为尽管设备方向如何,但仍需要强制界面方向的解决方法。我认为仍然不应该发生,因为解决方法仍然是对所提供方法的合法使用。这可能是 Apple 的旋转/方向堆栈中的错误。

    完整方法:

    - (void)forceOrientationChange
    {
        UIViewController *vc = [[UIViewController alloc] init];
    
        [UIViewController attemptRotationToDeviceOrientation]; /* Add this line */
    
        [self presentViewController:vc animated:NO completion:nil];
    
        [UIView animateWithDuration:.3 animations:^{
            [vc dismissViewControllerAnimated:NO completion:nil];
        } completion:nil];
    }
    

    【讨论】:

      【解决方案2】:

      当我们谈论方向时,它们是图片中的两件事:

      设备方向 接口方向 顾名思义,设备方向说明设备的方向,界面方向说明您的应用程序呈现其界面的方向。

      您正在做的是,您的应用支持所有方向。您必须勾选项目中的所有方向。

      现在,当您将设备的方向从纵向更改为横向时,当您以编程方式将 interfaceOrientation 设置为纵向模式时,就会发生这种情况。随着设备方向的变化,状态栏的方向也会发生变化。但是由于您将应用程序的界面限制为纵向,因此它不会改变其方向..

      所以你可以这样做:

      1. 取消选中您应用的横向支持并检查问题是否仍然存在。
      2. 让我知道您执行第一步时发生了什么:)

      【讨论】:

        【解决方案3】:

        使用此代码:

        UIApplication *application = [UIApplication sharedApplication];
        [application setStatusBarOrientation:UIDeviceOrientationLandscapeLeft 
                                    animated:YES];
        [[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeLeft];
        [super updateViewConstraints];
        [self.view updateConstraints];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多