【发布时间】: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];
}
所以,当您按下切换按钮时,这似乎工作正常,它会按预期改变方向。但是当我开始改变设备的实际方向时,就会出现问题。
重现问题的步骤:
- 以纵向打开应用程序
- 按下按钮强制将方向更改为横向(使实际设备保持纵向)
- 再次按下按钮强制旋转回纵向(仍然保持设备纵向)
- 无需按下按钮即可将实际设备旋转为横向
结果是视图不会旋转到横向,但状态栏会。
任何帮助将不胜感激!
【问题讨论】:
标签: ios objective-c rotation orientation