Iphone SetStatusBaroOrientation问题
我有一个导航控制器应用程序。首先我按下FirstViewController(支持纵向),然后按下SecondViewController(支持所有方向)。当我处于SecondViewController的横向模式并按下后退按钮时,FirstViewController将以横向模式显示。这就是我手动旋转导航视图的原因,但当我想将SetStatusBaroOrientation设置为纵向(第一个视图控制器应仅在纵向模式下显示)时,视图控制器的方向仍然是横向的,即使将设备旋转到纵向模式,方向仍然是横向的 .这是我的FirstViewController代码:Iphone SetStatusBaroOrientation问题,iphone,objective-c,uinavigationcontroller,uiinterfaceorientation,Iphone,Objective C,Uinavigationcontroller,Uiinterfaceorientation,我有一个导航控制器应用程序。首先我按下FirstViewController(支持纵向),然后按下SecondViewController(支持所有方向)。当我处于SecondViewController的横向模式并按下后退按钮时,FirstViewController将以横向模式显示。这就是我手动旋转导航视图的原因,但当我想将SetStatusBaroOrientation设置为纵向(第一个视图控制器应仅在纵向模式下显示)时,视图控制器的方向仍然是横向的,即使将设备旋转到纵向模式,方向仍然是横
- (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];
}
}
但当我旋转到肖像时,自我界面定向仍然停留在风景中。
但我确实需要手动将视图旋转到纵向模式,以允许用户看到FirstViewController只支持纵向。
我可以选择将SecondViewController的视图放在主窗口上(比如模态窗口),但我不喜欢这个想法,因为在我看来,如果苹果有SetStatusBaroOrientation方法,它必须正确地解决这个问题。我会去掉转换,使用
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:animated];
这与视图堆栈的强制重画相结合将完成此操作。这可以通过将以下内容添加到第一个控制器的ViewDidDisplay中来实现(它在viewWillApear中不起作用)
不幸的是,执行此操作时,过渡动画不是很清晰,因此我建议拍摄纵向屏幕的快照,将其覆盖在视图上,然后使用单独的动画将其淡出。我将取消转换,并使用
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:animated];
这与视图堆栈的强制重画相结合将完成此操作。这可以通过将以下内容添加到第一个控制器的ViewDidDisplay中来实现(它在viewWillApear中不起作用)
不幸的是,当你这样做时,过渡动画不是很清晰,所以我建议你拍摄一张纵向屏幕的快照,将其覆盖在你的视图上,然后用一个单独的动画将其淡出。史蒂文·维特马的回答对我不起作用 我有一个视图控制器,允许所有方向,其余的只支持纵向。当我使用横向视图中的第一个视图控制器并导航到另一个视图控制器时,方向并没有像您所经历的那样刷新 我发现了另一个重新加载视图的技巧,可以纠正方向。只需添加一个模态视图控制器,您甚至看不到它。在所有其他视图中添加此选项:
- (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];
....
}
史蒂文·韦尔特马的回答对我不起作用 我有一个视图控制器,允许所有方向,其余的只支持纵向。当我使用横向视图中的第一个视图控制器并导航到另一个视图控制器时,方向并没有像您所经历的那样刷新 我发现了另一个重新加载视图的技巧,可以纠正方向。只需添加一个模态视图控制器,您甚至看不到它。在所有其他视图中添加此选项:
- (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];
....
}
另一个快速解决方案是
另一个快速解决方案是