隐藏标签栏后在iOS7上显示黑色条纹

37

我正在使用这段代码来隐藏TabBar:

self.tabBarController.tabBar.hidden=YES;

我在我的项目中隐藏了tabBarController,但在iOS7中,在视图底部显示黑色条。当我返回到相同的视图时,它看起来很好。任何帮助将不胜感激。


你是否设置了布局约束或调整大小掩码?可能是视图没有调整大小,因此显示了控制器后面的空白区域。 - A'sa Dickens
请等一下,让我检查并告诉你。 - user2681789
我也有同样的问题?你遇到了什么解决方案吗? - nkongara
我有一些代码,它运行良好。你需要吗? - user2681789
在这里查看一个简单的解决方案 链接 - CeliaLi
10个回答

55

注意:这是针对iOS 6和7的解决方案。

在iOS 7中,为了扩展可点击区域并隐藏隐藏的UITabBar位置上的黑色条,您应该为您的UIViewController启用“扩展边缘 - 在不透明的栏下”选项。

扩展边缘 - 在不透明的栏下选项

或者,您可以编程设置此属性:

  

[self setExtendedLayoutIncludesOpaqueBars:YES]

这是一个隐藏或移动iOS 6/7的TabBar的代码示例:

UITabBarController *bar = [self tabBarController];
if ([self respondsToSelector:@selector(setExtendedLayoutIncludesOpaqueBars:)]) {
    //iOS 7 - hide by property
    NSLog(@"iOS 7");
    [self setExtendedLayoutIncludesOpaqueBars:YES];
    bar.tabBar.hidden = YES;
} else {
    //iOS 6 - move TabBar off screen
    NSLog(@"iOS 6");
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float height = screenRect.size.height;
    [self moveTabBarToPosition:height];
}

//Moving the tab bar and its subviews offscreen so that top is at position y
-(void)moveTabBarToPosition:(int)y {

    self.tabBarController.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);

    for(UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
        } else {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
            view.backgroundColor = [UIColor blackColor];
        }
    }
}

这篇文章中获取的将选项卡栏移动到屏幕外的函数。


这在我的iO8上有效,我没有看到我的普通TVC中的黑色条,但当我的UISearchController处于活动状态时,混乱的黑色条会显示出来。尝试了几个小时来解决...但是在我的TableviewController中勾选了这个简单的复选框就解决了。 - DogCoffee
在iOS8上,启用“在底部栏下”和“在不透明栏下”,它可以正常工作。 - GuramK
对我来说,在iOS8上工作正常,所有“扩展边缘”选项都已勾选! - db0
对我来说,在iOS 10上可以工作。 - Will

20

以下代码适用于我

- (void)showTabBar {
[self.tabBar setTranslucent:NO];
[self.tabBar setHidden:NO];
}

- (void)hideTabBar {
    [self.tabBar setTranslucent:YES];
    [self.tabBar setHidden:YES];
}

iOS 10 - 隐藏后黑色区域仍然存在 - Next Developer

13

试试这个:

- (BOOL)hidesBottomBarWhenPushed {
     return YES;
}

4

我在使用UINavigationController时遇到了一些问题:

这是适用于iOS 7和UINavigationControllers的解决方案:

头文件

@interface UITabBarController (HideTabBar)
- (void)setHideTabBar:(BOOL)hide animated:(BOOL)animated;
@end

实现

#import "UITabBarController+HideTabBar.h"

@implementation UITabBarController (HideTabBar)

- (void)setHideTabBar:(BOOL)hide animated:(BOOL)animated {
  UIViewController *selectedViewController = self.selectedViewController;
  /**
  * If the selectedViewController is a UINavigationController, get the visibleViewController.
  * - setEdgesForExtendedLayout won't work with the UINavigationBarController itself.
  * - setExtendedLayoutIncludesOpaqueBars won't work with the UINavigationBarController itself.
  */
  if ([selectedViewController isKindOfClass:[UINavigationController class]])
    selectedViewController = ((UINavigationController *)selectedViewController).visibleViewController;
  __weak __typeof(self) weakSelf = self;

  void (^animations)(void) = ^{
      selectedViewController.edgesForExtendedLayout = UIRectEdgeAll;
      [selectedViewController setExtendedLayoutIncludesOpaqueBars:hide];
      weakSelf.tabBar.hidden = hide;

      /**
      * Just in case we have a navigationController, call layoutSubviews in order to resize the selectedViewController
      */
      [selectedViewController.navigationController.view layoutSubviews];
  };

  [UIView animateWithDuration:animated ? UINavigationControllerHideShowBarDuration : 0 animations:animations];
}

@end

感谢Vadim Trulyaev指出的在不透明栏下延伸边缘标志!


1

一行Swift 3代码。

将以下内容放入你的UIViewController子类中:

    override var hidesBottomBarWhenPushed: Bool { get { return true }  set { self.hidesBottomBarWhenPushed = newValue }}

1

将控制器中的属性hidesBottomBarWhenPushed设置为true以进行隐藏。

要进行隐藏,需要将所有控制器放入prepare for segue中。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    segue.destination.hidesBottomBarWhenPushed = true
}

0
To showTabbar:

- (void)showTabBar:(UITabBarController *) tabbarcontroller
    {
            //[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, 521, view.frame.size.width, view.frame.size.height)];

                    }
            else
                    {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 521)];
                    }
                }

            // [UIView commitAnimations];
    }

To hide Tabbar:
 - (void)hideTabBar:(UITabBarController *) tabbarcontroller
    {
            //[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, 568, view.frame.size.width, view.frame.size.height)];
                    }
            else
                    {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 568)];
                    }
                }

            //[UIView commitAnimations];
    }

4
这确实隐藏了选项卡栏,但在iOS7中,选项卡栏原来所在的空间不可点击。 - animal_chin

0

我花了很长时间来解决这个问题,试图在表视图底部放置一个响应按钮。我没有使用自动布局。我发现iOS 6和7之间有两个主要区别:

  1. iOS7中,当标签栏退出动画时,根视图控制器的视图不会延伸到标签栏所在的区域;它需要被重新调整大小。

  2. iOS7只需要对UITabBar类型的视图进行屏幕内外的动画操作。

另一个问题是,如果在iOS7中,您将可见视图控制器的子视图扩展到标签视图留下的空间上,除非也扩展主视图,否则它不会产生交互作用。考虑到这一点,我使用了以下代码:

隐藏标签栏(反转数学计算以显示它):

[UIView animateWithDuration:kHideTabBarAnimationDuration animations:^{
    for(UIView *view in self.tabBarController.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y + view.frame.size.height, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
            if (![MYDeviceUtility systemVersionGreaterThanOrEqualTo:@"7.0"])
            {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height + self.tabBarController.tabBar.frame.size.height)];
            }
        }
    }
} completion:nil];

隐藏选项卡栏时调整主视图:

// Expand view into the tab bar space
if ([MYDeviceUtility systemVersionGreaterThanOrEqualTo:@"7.0"])
{
    CGRect frame = self.view.frame;
    self.view.frame = CGRectMake(frame.origin.x,
                                 frame.origin.y,
                                 frame.size.width,
                                 frame.size.height + tabBarHeight);
}

显示选项卡栏时调整主视图:

[UIView animateWithDuration:kHideTabBarAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
    // Create space for the tab bar            
    if ([MYDeviceUtility systemVersionGreaterThanOrEqualTo:@"7.0"])
    {
        CGRect frame = self.view.frame;
        self.view.frame = CGRectMake(frame.origin.x,
                                     frame.origin.y,
                                     frame.size.width,
                                     frame.size.height - tabBarHeight);
    }
} completion:nil];

请注意,当隐藏选项卡栏时,我不会动画化主视图的扩展,因为这看起来很自然,扩展发生在选项卡栏后面。
此外,请注意,在iOS 7中,如果您在隐藏选项卡栏时从纵向旋转到横向,则黑色框将重新出现。我通过在旋转动画之前将选项卡栏动画回屏幕上解决了这个问题(对于我正在工作的内容已足够)。

0

基于 @Vadim Trulyaev 的解决方案,我创建了一个简单的用法:

UITabBarController+HideTabBar.h

@interface UITabBarController (Additions)
- (void)setTabBarHidden:(BOOL)hidden myClass:(UIViewController *)myClass;
@end

UITabBarController+HideTabBar.m

#import "UITabBarController+HideTabBar.h"

@implementation UITabBarController (HideTabBar)

- (void)setTabBarHidden:(BOOL)hidden myClass:(UIViewController *)myClass{
    if ([myClass respondsToSelector:@selector(setExtendedLayoutIncludesOpaqueBars:)]) {
        //iOS 7 - hide by property
        NSLog(@"iOS 7");
        [myClass setExtendedLayoutIncludesOpaqueBars:hidden];
        self.tabBar.hidden = hidden;
    } else {
        //iOS 6 - move TabBar off screen
        NSLog(@"iOS 6");
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        float height = screenRect.size.height;
        if(hidden){
            [self moveTabBarToPosition:height];
        }else{
            [self moveTabBarToPosition:height - self.tabBar.frame.size.height];
        }
    }
}

//Moving the tab bar and its subviews offscreen so that top is at position y
-(void)moveTabBarToPosition:(int)y {
    self.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBar.frame.size.width, self.tabBar.frame.size.height);

    for(UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
        } else {
            NSLog(@"%f",view.frame.size.height);
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
            NSLog(@"%f",view.frame.size.height);
            view.backgroundColor = [UIColor blackColor];
        }
    }
}

@end

如何使用:

[[self tabBarController] setTabBarHidden:NO myClass:self];

但是,在iOS6中我遇到了一些问题。当我第一次进入隐藏标签栏的ViewController1时,一切都运行良好,但是如果我进入一个显示标签栏的ViewController2,然后返回到需要隐藏标签栏的ViewController1时,黑色的空白会出现。有人可以帮助我吗?

谢谢!


0
除了其他出色的建议外,以下建议可能会对某些人有所帮助。尝试在awakeFromNib中将tabbar设置为隐藏,而不是在生命周期的后期。我发现隐藏的tabbar在转场时会闪烁黑色,这个方法解决了我的问题。
- (void)awakeFromNib { [super awakeFromNib]; self.tabBarController.tabBar.hidden = YES; }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接