【问题标题】:hiding TabBar when rotating iPhone device to landscape将 iPhone 设备旋转为横向时隐藏 TabBar
【发布时间】:2010-11-12 22:50:05
【问题描述】:

这就是我所拥有的: 一个处理不同 UIViewControllers 的 UITabBarController。在其中一个 UIViewController 中,我试图切换设备旋转到横向时显示的视图。 重要的部分是横向显示的视图必须占据整个屏幕......

我已经正确实现了方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

事实上,我确实正确地进行了轮换,并且我的观点交换了。 我什至隐藏了状态栏、导航栏和标签栏但我在屏幕底部一直有一个空白区域,这是标签栏的位置......

所以我假设设置 tabBar 的 hidden 属性不足以在整个屏幕上显示视图。我认为在 TabBarController 甚至 MainWindow 中有一些事情要做,比如“我现在不需要 TabBarController”。但我不知道如何正确解决这个问题。

如果有人遇到过这个问题,我将不胜感激。

谢谢你, 萨米人。

【问题讨论】:

标签: iphone uitabbarcontroller rotation uitabbar tabbarcontroller


【解决方案1】:

这对我有用。


- (void)viewDidLoad {
    [super viewDidLoad];    
    previousRect = self.view.frame; 
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {       
        [self.navigationController setNavigationBarHidden:TRUE animated:FALSE]; 
        [[UIApplication sharedApplication] setStatusBarHidden:TRUE animated:FALSE];
    }
    else
    {
        [self.navigationController setNavigationBarHidden:FALSE animated:FALSE];
        [[UIApplication sharedApplication] setStatusBarHidden:FALSE animated:FALSE];
    }
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;

    if ( self.tabBarController.view.subviews.count >= 2 )
    {
        UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0];
        UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];

        if(toOrientation == UIInterfaceOrientationLandscapeLeft || toOrientation == UIInterfaceOrientationLandscapeRight) {                 
            transView.frame = CGRectMake(0, 0, 480, 320 );
            tabBar.hidden = TRUE;
        }
        else
        {               
            transView.frame = previousRect;     
            tabBar.hidden = FALSE;
        }
    }
}

【讨论】:

  • 使用YES/NO 而不是TRUE/FALSE。虽然它有效,但它不是惯例。
【解决方案2】:

此代码运行良好,但是当我关闭模态显示的 uiviewcontroller 时,我的视图位于状态栏下方 20 像素。 我的视图在一个导航控制器内,所以我不会在旋转之前隐藏它。

【讨论】:

  • 我发现问题与导航栏有关,并且仅在 IOS 5 中。如果您遇到类似问题,只需隐藏并显示您的导航栏,然后在 viewwillappear 中显示。
【解决方案3】:
  • 上面的解决方案也对我有用,只是对 iOS6 及更高版本进行了一些小改动:
  • 在 iOS6 中删除以下行:“previousRect = self.view.frame;”
  • 还将“animated:”替换为“withAnimation:”
  • 并从底部删除“transView.frame = previousRect;”(在 else 函数中)
  • 这样对我有用。非常感谢用户 UB。

【讨论】:

    【解决方案4】:

    我需要标签栏在横向视图中进入全屏模式,我尝试使用上面建议的方法

    transView.frame = CGRectMake(0, 0, 480, 320 );

    这被证明是一个 hacky 解决方案,并带来了许多问题,例如隐藏和重新显示状态栏(退出纵向视图后重新显示时,视图会与状态栏重叠)。我不会推荐这个。最后对我来说完美的工作是推动一个包含横向视图的新视图控制器并使用委托来重用原始 VC 的功能。

    【讨论】:

      【解决方案5】:

      如果你有你的 UITabBarController 然后在里面放一个 UINavigationController 然后你可以使用 hidesBottomBarWhenPushed(有点诡计)来做到这一点。

      - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
          [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
      
          if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
              self.hidesBottomBarWhenPushed = NO;
              self.navigationController.viewControllers = self.navigationController.viewControllers;
              [self transitionToGridLayout];
          }
          else {
              self.hidesBottomBarWhenPushed = YES;
              self.navigationController.viewControllers = self.navigationController.viewControllers;
              [self transitionToCoverflowLayout];
          }
      }
      

      诀窍是推动您的视图控制器,以便拾取hidesBottomBarWhenPushed 标志。您可以使用以下内容。

      self.navigationController.viewControllers = self.navigationController.viewControllers;
      

      【讨论】:

        【解决方案6】:

        这种方法对我有用:

        - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        
            UIView *parentView = self.tabBarController.view;
            CGRect frame = parentView.frame;
            CGFloat windowHeight = parentView.window.frame.size.height;
        
            switch (toInterfaceOrientation) {
                case UIInterfaceOrientationLandscapeLeft:
                case UIInterfaceOrientationLandscapeRight:
                    CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
                    frame.size.height = windowHeight + tabBarHeight;
                    break;
                default:
                    frame.size.height = windowHeight;
                    break;
            }
        
            [UIView animateWithDuration:duration animations:^{
                parentView.frame = frame;
            }];
        }
        

        (仅在 iOS8 中测试。)

        【讨论】:

        • 我在生产应用程序中运行这段代码就好了。你在看什么?哪个版本的 iOS 8?
        • 试过 iOS 8.2 模拟器标签栏在旋转过程中显示。顺便说一句,逻辑是什么:当旋转到横向标签栏框架高度时,窗口高度会增加?
        • 尚未在模拟器中尝试过(我正在使用不是为该平台构建的框架),但在 iOS 8.1/8.2/8.3 的设备上运行良好。逻辑是在旋转到横向时将窗口高度增加标签栏高度,以使窗口比设备屏幕高到足以使标签栏呈现在屏幕外(并在旋转回纵向时撤消高度更改)。
        【解决方案7】:

        子类化您的 TabBarController 并在需要时隐藏 TabBar:

        class tabBarVC: UITabBarController {
        
            override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
                if size.height < size.width {
                    self.tabBar.hidden = true
                } else {
                    self.tabBar.hidden = false
                }
            }
        
        }
        

        【讨论】:

          【解决方案8】:

          也许你想用这个

          - (void)willAnimateRotationToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
          {
              [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
          
              __block UIView *weakTabBar = [self.tabBarController.view.subviews objectAtIndex:1];
              weakTabBar.alpha = 0;
              [UIView animateWithDuration:duration
                                    delay:0
                                  options:UIViewAnimationOptionCurveEaseIn // slow at the beggining
                               animations:^{
                                   weakTabBar.alpha = 1;
                               }
                               completion:^(BOOL finished) {
                                   weakTabBar.alpha = 1;
                               }];
              }
          

          }

          这不会隐藏标签栏,但会使旋转动画更流畅。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-05
            • 2015-03-20
            • 2023-03-14
            • 2021-12-27
            • 1970-01-01
            • 1970-01-01
            • 2012-06-17
            • 1970-01-01
            相关资源
            最近更新 更多