我有一个导航 Controller 应用程序。首先我推送 FirstViewController(支持纵向),然后推送 SecondViewController(支持所有方向)。当我处于 SecondViewController 的横向模式并按后退按钮时,FirstViewController 以横向模式显示。这就是为什么我手动旋转导航 View ,但是当我想将 setStatusBarOrientation 设置为纵向(第一个 View Controller 应该仅在纵向模式下出现)时, View Controller 的方向仍然是横向的,即使将设备旋转到纵向模式,方向保持横向 .这是我的 FirstViewController 代码:
- (void)viewWillAppear:(BOOL)animated
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
prevInterfaceOrientation = UIInterfaceOrientationLandscapeLeft;
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform =
CGAffineTransformMakeRotation(degreesToRadians(90));
}
else if (self.interfaceOrientation ==
UIInterfaceOrientationLandscapeRight) {
prevInterfaceOrientation = UIInterfaceOrientationLandscapeRight;
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform =
CGAffineTransformMakeRotation(degreesToRadians(-90));
}
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
[self.tableViewDetail reloadData];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
if (prevInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
self.navigationController.view.transform = CGAffineTransformIdentity;
}
else if (prevInterfaceOrientation ==
UIInterfaceOrientationLandscapeRight) {
self.navigationController.view.transform = CGAffineTransformIdentity;
}
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
[self.tableViewDetail reloadData];
}
}
我什至尝试使用:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(prevInterfaceOrientation))
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}
}
但是当我旋转到纵向时,self.interfaceOrientation 仍然保持横向。 但我确实需要手动将 View 旋转到纵向模式,以允许用户看到 FirstViewController 仅支持纵向方向。 我可以选择将 SecondViewController 的 View 放在 MainWindow 上(如模态窗口),但我不喜欢这个想法,因为如果苹果有 setStatusBarOrientation 方法,在我看来,它必须正确解决这个问题。
最佳答案
我会摆脱转换,并使用
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:animated];
这与 View 堆栈的强制重绘相结合将完成它。这可以通过将以下内容添加到第一个 Controller 的 viewDidAppear 来完成(它在 viewWillApear 中不起作用)。
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
if ([window.subviews count] > 0) {
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window insertSubview:view atIndex:0];
}
else {
DLog(@"NO view to force rotate?");
}
不幸的是,当您执行此操作时,过渡动画不是很干净,因此我建议拍摄纵向屏幕的快照,将其覆盖在您的 View 上,然后使用单独的动画将其淡出。
关于iphone - setStatusBarOrientation问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10796210/