隐藏tabBar显示Ios7中的黑条

时间:2013-10-07 12:53:00

标签: ios xcode

我正在使用此代码隐藏TabBar:

self.tabBarController.tabBar.hidden=YES;

我在我的项目中隐藏了tabBarController。但它在Ios7的视图底部显示了黑条。当我回到相同的视图时它看起来很好。非常感谢。

10 个答案:

答案 0 :(得分:52)

注意:它仅适用于iOS6和7。

在iOS 7中,为了扩展可点击区域并在隐藏的UITabBar上隐藏黑条,你应该为你的UIViewController启用'Extend Edges - Under Opaque Bars'选项。

Extend Edges - Under Opaque Bars option

或者您可以通过编程方式设置此属性:

  

[self setExtendedLayoutIncludesOpaqueBars:YES]

以下是隐藏或移动TabBar for iOS 6/7的代码示例:

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

this post获取移动Tab键的功能。

答案 1 :(得分:20)

下一段代码适合我

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

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

答案 2 :(得分:13)

试试这个:

- (BOOL)hidesBottomBarWhenPushed {
     return YES;
}

答案 3 :(得分:4)

使用UINavigationController时遇到了一些麻烦:

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

HeaderFile

@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指出延伸边 - 不透明条标志!

答案 4 :(得分:1)

一行Swift 3回答。

将以下内容放在UIViewController子类中:

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

答案 5 :(得分:1)

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

为隐藏起见,您所有的控制器都放入了prepare for segue

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

答案 6 :(得分: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];
    }

答案 7 :(得分:0)

我花了很长时间与此作斗争,试图在桌面视图的底部放置一个响应按钮。我没有使用自动布局。我发现iOS 6和7之间存在两个主要区别:

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

  2. 在iOS7上,只需要在屏幕上关闭UITabBar类型的视图。

  3. 第1点的另一个问题是,如果在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中,如果在隐藏标签栏时从纵向旋转到横向,则会再次出现黑框。我通过在旋转动画之前将标签栏动画回屏幕来解决这个问题(这对于我正在处理的事情已经足够了)。

答案 8 :(得分: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时,tabbar被隐藏everthing工作正常,但是如果我去ViewController2显示标签栏并返回ViewController1标签栏必须隐藏,黑色空间显示。有人可以帮助我吗?!

谢谢!

答案 9 :(得分:0)

除了其他优秀建议之外,以下建议可能会帮助某人。尝试将标签栏设置为隐藏在awakeFromNib中,而不是在生命周期的后期。我发现隐藏的tabbar在segue上闪烁着黑色,这为我修复了它。

- (void)awakeFromNib { [super awakeFromNib]; self.tabBarController.tabBar.hidden = YES; }

相关问题