【问题标题】:setStatusBarOrientation issuesetStatusBarOrientation 问题
【发布时间】:2012-06-03 12:04:39
【问题描述】:

我有一个导航控制器应用程序。首先我推 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 上(如模式窗口),但我不喜欢这个想法,因为如果苹果有 setStatusBarOrientation 方法,在我看来,它必须正确解决这个问题。

【问题讨论】:

    标签: iphone objective-c uinavigationcontroller uiinterfaceorientation


    【解决方案1】:

    另一个快速解决方案是

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        我会摆脱转换,并使用

              [[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?");
               }
        

        不幸的是,当您这样做时,过渡动画不是很干净,因此我建议您拍摄纵向屏幕的快照,将其覆盖在您的视图上,然后使用单独的动画将其淡出。

        【讨论】:

          猜你喜欢
          • 2014-11-18
          • 2012-08-15
          • 2011-06-02
          • 1970-01-01
          • 1970-01-01
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 2016-01-22
          相关资源
          最近更新 更多