我有一个导航控制器应用程序。首先,我按FirstViewController(支持纵向),然后按SecondViewController(支持所有方向)。当我处于第二视图控制器的横向模式并按返回按钮时,第一视图控制器以横向模式出现。这就是为什么我手动旋转导航视图,但是当我想将 setStatusBar方向设置为纵向(第一个视图控制器应该只出现在纵向模式下)时,视图控制器的方向仍然是横向,即使将设备旋转到纵向模式,方向仍然是横向.这是我的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.interface方向仍然保持横向。但是我真的需要手动将视图旋转到纵向模式以允许用户看到,FirstViewController仅支持纵向。我可以选择将 SecondViewController 的视图放在 MainWindow(如模态窗口)上,但我不喜欢这个想法,因为如果苹果有 setStatusBarOrientation 方法,在我看来,它必须正确解决这个问题。
我会摆脱转换,并使用
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:animated];
这与视图堆栈的强制重绘相结合即可完成。 这可以通过将以下内容添加到 viewDidSeem 到第一个控制器来完成(它在 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?");
}
不幸的是,当您执行此操作时,过渡动画不是很干净,因此我建议您拍摄纵向屏幕的快照,将其覆盖在您的视图上,然后用单独的动画将其淡出。
Steven Veltema的回答对我不起作用。
我有一个视图控制器,其中所有方向都允许,其余的仅支持纵向。当我将第一个视图控制器横向并导航到另一个视图控制器时,方向不会像您遇到的那样刷新。
我找到了另一个技巧来重新加载我正确方向的视图。只需添加一个您甚至看不到它的模态视图控制器。在所有其他视图中添加以下内容:
- (void)viewDidAppear:(BOOL)animated
{
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
UIViewController * viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[viewController dismissModalViewControllerAnimated:NO];
}
[super viewDidAppear:animated];
....
}
另一个快速的解决方案是
- 单击左侧栏中的项目。 在
- 常规设置中,选择"在应用程序启动期间隐藏"选项。