setStatusBarOrientation问题

时间:2012-05-29 09:13:45

标签: iphone objective-c uinavigationcontroller uiinterfaceorientation

我有一个导航控制器应用程序。首先,我推送FirstViewController(支持纵向方向),然后推送SecondViewController(支持所有方向)。当我处于SecondViewController的横向模式并按下后退按钮时,FirstViewController以横向模式显示。这就是我手动旋转导航视图的原因,但是当我想将setStatusBarOrientation设置为Portrait(第一个视图控制器应该仅在纵向模式下出现)时,视图控制器的方向仍然是横向的,即使将设备旋转到纵向模式,方向保持景观 这是我的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仍然保持风景。 但我真的需要手动将视图旋转到纵向模式以允许用户看到,FirstViewController仅支持纵向方向。 我可以选择将SecondViewController的视图放在MainWindow上(比如模态窗口),但我不喜欢这个想法,因为如果apple有setStatusBarOrientation方法,在我看来,它必须正确解决这个问题。 / p>

3 个答案:

答案 0 :(得分:4)

我会摆脱转换,并使用

      [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:animated];

这与强制重绘视图堆栈相结合将完成它。这可以通过将以下内容添加到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?");
       }

不幸的是,当你这样做时,过渡动画不是很干净,所以我建议拍摄人像屏幕的快照,将其覆盖在你的视图上,然后用一个单独的动画将其淡出。

答案 1 :(得分:0)

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];
    ....
}

答案 2 :(得分:0)

另一个快速解决方案是

  1. 点击左侧栏中的项目。
  2. 在常规设置中,选择“在应用程序启动期间隐藏”选项。