Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编程接口方向更改不适用于iOS_Ios_Objective C_Rotation_Orientation - Fatal编程技术网

编程接口方向更改不适用于iOS

编程接口方向更改不适用于iOS,ios,objective-c,rotation,orientation,Ios,Objective C,Rotation,Orientation,所以,我有一个项目,当用户按下按钮时,我需要强制改变方向。我创建了一个示例来演示这个问题 @interface DefaultViewController () { UIInterfaceOrientation _preferredOrientation; } 一些旋转处理位 - (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPr

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

@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];
}
所以,当你按下切换按钮时,这看起来很好,它会像预期的那样改变方向。但当我开始改变设备的实际方向时,出现了一个问题

重现问题的步骤:

  • 以纵向方向打开应用程序
  • 按下按钮强制将方向更改为横向(使实际设备保持纵向)
  • 再次按下按钮以强制旋转回到纵向(仍保持设备处于纵向)
  • 将实际设备旋转至横向,无需按下按钮
  • 结果是视图不会旋转为横向,但状态栏会旋转


    任何帮助都将不胜感激

    当我们谈论方向时,有两件事进入了我们的视野:

    设备定向 界面定向 正如名称所示,设备方向说明了设备的方向,界面方向说明了应用程序显示界面的方向

    在这里,你要做的是,你的应用程序支持所有方向。您必须选中项目中的所有方向

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

    所以这就是你能做的:

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

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

    [UIViewController尝试旋转到设备定向];
    
    很难说这到底是为什么。此调用要求通过调用
    shouldAutorotateToInterfaceOrientation:
    以及返回
    YES
    时的后续旋转方法,尝试将
    接口方向
    设备方向
    匹配

    界面和设备方向似乎不同步,这是因为尽管设备方向不同,但仍需要解决强制界面方向的问题。但在我看来,仍然不应该发生,因为变通方法仍然是对所提供方法的合法使用。这可能是苹果旋转/定向堆栈中的一个bug

    完整方法:

    -(无效)强制方向更改
    {
    UIViewController*vc=[[UIViewController alloc]init];
    [UIViewController attemptRotationToDeviceOrientation];/*添加此行*/
    [self-presentViewController:vc动画:未完成:无];
    [UIView animateWithDuration:.3个动画:^{
    [vc DismissViewControllerInitiated:未完成:无];
    }完成:无];
    }
    
    使用以下代码:

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