【问题标题】:How to hide iOS tab bar for a particular tab index and tab is shown back when the screen is touched?如何隐藏特定标签索引的iOS标签栏,并在触摸屏幕时显示标签?
【发布时间】:2011-11-28 02:45:12
【问题描述】:

我试图隐藏特定视图的标签栏并在触摸屏幕时将其显示回来。我希望它具有与 youtube 相似的效果,例如,当播放视频时,播放器控件被隐藏并且触摸屏幕时再次显示控件。

【问题讨论】:

标签: ios uitabbarcontroller


【解决方案1】:

您可以使用此代码来显示和隐藏标签栏:

@implementation UITabBarController (Extras)
-(void)showTabBar:(BOOL)show {
  UITabBar* tabBar = self.tabBar;
  if (show != tabBar.hidden)
    return;
  UIView* subview = [self.view.subviews objectAtIndex:0];
  CGRect frame = subview.frame;
  frame.size.height += tabBar.frame.size.height * (show ? -1 : 1);
  subview.frame = frame;
  tabBar.hidden = !show;
}

此代码有效,最近被 Apple 在一个应用程序中接受,并且(作为一个类别)我发现比其他解决方案更易于使用。

当你想隐藏 tabBar 时,只需调用:

[self.tabBarController showTabBar:NO];

同样,要再次显示,请使用YES 作为参数调用此消息。

注意:不知何故,我忘记了我在过去的某个时候已经查看过此代码,现在我不确定最初是谁回答了它。索拉布answered a similar question。 Saurabh 提供的代码会遍历所有视图以查找 isKindOfClass:[UITabBar class],而我只获取第一个子视图——它在更新时可能很脆弱。

【讨论】:

    【解决方案2】:

    您是否尝试过直接隐藏标签栏? 在视图控制器中,您希望没有标签栏,将tabBar.hidden = YES; 添加到viewWillAppear:viewDidAppear: 以反转具有内部触摸事件触发器tabBar.hidden = NO;

    我没有亲自用标签栏做过这个,但这适用于其他视图,所以这是我首先尝试的方式。

    【讨论】:

    • 确实隐藏了标签栏,但我现在在屏幕底部得到了一个 49 像素高的空白区域。视图不会自动调整大小以填充它,并且尝试手动更改视图框架没有任何作用。
    • @AndrewS 看看这个问题stackoverflow.com/questions/3807255/…
    • 这是推送视图;我想要它作为标签栏中的一个标签。我最终使用了How to hide uitabbarcontroller 中的代码
    • 这在 iOS 7 中很好。iOS 6 需要框架黑客。
    【解决方案3】:
       Try This Code
    
    - (void) hideTabBar:(UITabBarController *) tabbarcontroller 
     {
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    float fHeight = screenRect.size.height;
    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width;
    }
    
    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
        } 
        else 
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
            view.backgroundColor = [UIColor blackColor];
        }
    }
    [UIView commitAnimations];
     }
    
    
    
       - (void) showTabBar:(UITabBarController *) tabbarcontroller 
      {   
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float fHeight = screenRect.size.height - 49.0;
    
    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width - 49.0;
    }
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in tabbarcontroller.view.subviews)
    {   
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];            
        } 
        else 
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
        }       
    }
     [UIView commitAnimations]; 
    }
    

    【讨论】:

      【解决方案4】:

      试试这个:

      self.tabBarController.tabbar.hidden = YES;
      

      把它放在 viewDidLoad 中。

      【讨论】:

        猜你喜欢
        • 2019-02-15
        • 2021-06-01
        • 2016-05-27
        • 2021-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多