隐藏tabos显示Ios7中的黑条

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

self.tabBarController.tabBar.hidden=YES; 

我隐藏tabBarController在我的项目。但它显示黑色酒吧在视图底部的Ios7。当我回到相同的看法,它看起来不错。任何帮助将不胜感激。

注意:它仅适用于iOS6和7的解决scheme。

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

移动标签栏离屏的function从这篇文章中获得 。

接下来的代码适用于我

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

尝试这个:

 - (BOOL)hidesBottomBarWhenPushed { return YES; } 

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

这是我的解决scheme适用于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指出了Extend Edges – 在不透明酒吧旗下!

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

我花了很长时间来对抗这个,试图在桌子视图的底部放置一个响应式button。 我没有使用自动布局。 我发现了iOS 6和7之间的两个主要区别:

  1. 在iOS7上,当标签栏dynamic显示时,根视图控制器的视图不会扩展到标签栏所在的区域; 它需要resize。

  2. 在iOS7上,只有UITabBartypes的视图需要在屏幕上animation化。

第一点的另一个问题是,如果在iOS7中,您将可见视图控制器主视图的子视图扩展到选项卡视图留下的空间上,那么除非主视图被扩展,否则它将不可交互。 考虑到这一点,我使用了下面的代码:

隐藏标签栏(反转显示math):

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

请注意,当隐藏标签栏时,我不会为主视图展开设置animation,这看起来很自然,因为展开发生在标签栏的后面。

另请注意

在iOS 7中,如果在隐藏标签栏的情况下从纵向旋转到横向,则会再次出现黑框。 我通过在旋转animation之前将选项卡animation返回到屏幕上来解决这个问题(对于我正在进行的工作来说,这足够好了)。

基于@Vadim Trulyaev的解决scheme,我创build了一个简单的用法:

的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的工作正常,但如果我去一个ViewController2显示选项卡栏,并返回到ViewController1标签栏必须隐藏,黑色空间显示出来。 任何人都可以帮助我?

谢谢!

除了其他优秀的build议,下面的build议可能会帮助别人。 试着将你的tabbar设置为隐藏在awakeFromNib中,而不是在生命周期的后面。 我发现隐藏的tabbar在segue上闪烁着黑色,这为我修好了。

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

一行Swift 3的答案。

把下面的内容放在你的UIViewController子类中:

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