编程接口方向更改不适用于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];