如何隐藏uitabbarcontroller

我有一个UITabBarController的问题。 在我的应用程序中,我想隐藏它,但不使用hidesBottomBarWhenPushed因为我想隐藏它,而不是当我推它。 例如,当我在应用程序中按隐藏button时,我想隐藏它。

我在谷歌阅读了很多文章,但我无法find如何做到这一点。

我从我的工作代码粘贴这个…你可以调用这些方法来隐藏和显示tabbarcontroller ….只是将tabbarcontroller实例传递给这些函数..

 // Method call [self hideTabBar:self.tabBarController]; 

 // Method implementations - (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, 480, view.frame.size.width, view.frame.size.height)]; } else { [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)]; } } [UIView commitAnimations]; } - (void)showTabBar:(UITabBarController *) tabbarcontroller { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; for(UIView *view in tabbarcontroller.view.subviews) { NSLog(@"%@", view); if([view isKindOfClass:[UITabBar class]]) { [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)]; } else { [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)]; } } [UIView commitAnimations]; } 

修改后的Setomidor的答案可以在横向,纵向和iPad上使用(320和480值仅适用于iPhone)。

 - (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 - tabbarcontroller.tabBar.frame.size.height; if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) ) { fHeight = screenRect.size.width - tabbarcontroller.tabBar.frame.size.height; } [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]; } 

还修改了代码,以处理在iOS 6中引入的UIDevice方向更改的更改,并确保它即使在设备仰卧时也能正常工作。

在你的button的操作方法中:

 [self.tabBarController.tabBar setHidden:YES]; 

Saurahb和karlbecker_com的解决scheme非常棒,尽pipe当视图包含tableview时,它们可能会引起明显的popup效果,而tab bar会进行animation备份。 我做了一些修改,并将其组合成一个单一的function(作为UITabBarController类别)。 这不完全完美(延迟校正animation),但表格给出了很好的结果。

如果您喜欢animation块和类别,请尝试一下。 方向和设备友好。

的UITabBarController + ShowHideBar.m:

 #import "UITabBarController+ShowHideBar.h" @implementation UITabBarController (ShowHideBar) - (void) setHidden:(BOOL)hidden{ CGRect screenRect = [[UIScreen mainScreen] bounds]; float fHeight = screenRect.size.height; if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) ){ fHeight = screenRect.size.width; } if(!hidden) fHeight -= self.tabBar.frame.size.height; [UIView animateWithDuration:0.25 animations:^{ for(UIView *view in self.view.subviews){ if([view isKindOfClass:[UITabBar class]]){ [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)]; }else{ if(hidden) [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)]; } } }completion:^(BOOL finished){ if(!hidden){ [UIView animateWithDuration:0.25 animations:^{ for(UIView *view in self.view.subviews) { if(![view isKindOfClass:[UITabBar class]]) [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)]; } }]; } }]; } @end 

的UITabBarController + ShowHideBar.h:

 #import <UIKit/UIKit.h> @interface UITabBarController (ShowHideBar) - (void) setHidden:(BOOL)hidden; @end 

用法:

 [self.tabBarController setHidden:YES]; [self.tabBarController setHidden:NO]; 

上面的Saurabh的答案可以扩展到横向:

 + (void) hideTabBar:(UITabBarController *) tabbarcontroller { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; //Support for landscape views int orientation = [[UIDevice currentDevice] orientation]; int x_pos = 480; if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { x_pos = 320; } for(UIView *view in tabbarcontroller.view.subviews) { if([view isKindOfClass:[UITabBar class]]) { [view setFrame:CGRectMake(view.frame.origin.x, x_pos, view.frame.size.width, view.frame.size.height)]; } else { [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, x_pos)]; } } [UIView commitAnimations]; } 

`

showTabBar()的相应的x_pos数字是431271

@karlbecker_com Answer对于iPhone 4和iPhone 5来说都是完美的。如果任何人在iOS7黑条的底部出现问题,请将tabBarController设置为半透明

 #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) // To Hide the black line in IOS7 only, this extra bit is required if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { [self.tabBarController.tabBar setTranslucent:YES]; } 

这是karlbecker_com的答案,移植到MonoTouch(Xamarin.iOS)。 唯一的区别是我在一个inheritance自UITabBarController的类上实现了方法,因此对“ tabbarcontroller ”的引用被replace为“ this ”。

 public void HideTabBar() { var screenRect = UIScreen.MainScreen.Bounds; float fHeight = screenRect.Height; if(UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeLeft || UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeRight) { fHeight = screenRect.Width; } UIView.BeginAnimations(null); UIView.SetAnimationDuration(0.4); foreach(UIView view in this.View.Subviews) { if(view is UITabBar) { view.Frame = new RectangleF(view.Frame.X, fHeight, view.Frame.Width, view.Frame.Height); } else { view.Frame = new RectangleF(view.Frame.X, view.Frame.Y, view.Frame.Width, fHeight); view.BackgroundColor = UIColor.Black; } } UIView.CommitAnimations(); } public void ShowTabBar() { var screenRect = UIScreen.MainScreen.Bounds; float fHeight = screenRect.Height - 49f; if(UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeLeft || UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeRight) { fHeight = screenRect.Width - 49f; } UIView.BeginAnimations(null); UIView.SetAnimationDuration(0.4); foreach(UIView view in this.View.Subviews) { if(view is UITabBar) { view.Frame = new RectangleF(view.Frame.X, fHeight, view.Frame.Width, view.Frame.Height); } else { view.Frame = new RectangleF(view.Frame.X, view.Frame.Y, view.Frame.Width, fHeight); } } UIView.CommitAnimations(); } 

你可以推一个模态视图控制器

 [self presentModalViewController:myFullscreenViewController animated:YES]; 

这将在您当前的屏幕上创build一个全屏的全新视图。

解雇与dismissModalViewController:animated: ist dismissModalViewController:animated:

下面的解决scheme适用于我完全相同的用例,我必须使用TabBaranimation移动到全屏模式。

基本上,这个想法是

  1. 创build一个UITabBar的快照;

  2. 将快照的UIImage添加到与UITabBar具有相同框架的UIImageView ;

  3. 调整基础视图的大小并将其放在self.tabBarController.view ;

  4. 设置UITabBar的alpha为0.0;

  5. UIImageViewUITabBar的快照放在self.tabBarController.view ;

  6. 一旦达到上述目的,就做任何一种animation

     #import "QuartzCore/CALayer.h" @implementation FTBFirstViewController { BOOL hidden; UIImageView *fakeTabBarImageView; UIView *viewToResize; } - (void)viewDidLoad { [super viewDidLoad]; ////////////////////////////// // Create your viewToResize ////////////////////////////// [self.view addSubview:viewToResize]; hidden = NO; } - (void)hideTabBar:(id)sender { if (!hidden) { // // to create the fake UITabBar fakeTabBarImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; UIImage *fakeTabBarImage = [self imageScreenshotFromView:self.tabBarController.tabBar]; fakeTabBarImageView.image = fakeTabBarImage; fakeTabBarImageView.frame = self.tabBarController.tabBar.frame; // // to resize underlying UIView viewToResize.frame = (CGRect){viewToResize.frame.origin.x, viewToResize.frame.origin.y + 20.f, viewToResize.frame.size.width, viewToResize.frame.size.height + fakeTabBarImageView.frame.size.height}; // // to hide real UITabBar self.tabBarController.tabBar.alpha = 0.0; // // to add views in exactly this order [self.tabBarController.view addSubview:viewToResize]; [self.tabBarController.view addSubview:fakeTabBarImageView]; // // do any sort of animation [UIView animateWithDuration:0.8 animations:^{ fakeTabBarImageView.frame = (CGRect){fakeTabBarImageView.frame.origin.x, fakeTabBarImageView.frame.origin.y + fakeTabBarImageView.frame.size.height, fakeTabBarImageView.frame.size}; }]; hidden = YES; } else { [UIView animateWithDuration:0.8 animations:^{ fakeTabBarImageView.frame = (CGRect){fakeTabBarImageView.frame.origin.x, fakeTabBarImageView.frame.origin.y - fakeTabBarImageView.frame.size.height, fakeTabBarImageView.frame.size}; } completion:^(BOOL complete){ self.tabBarController.tabBar.alpha = 1.0; [fakeTabBarImageView removeFromSuperview]; fakeTabBarImageView = nil; viewToResize.frame = self.view.frame; [self.view addSubview:viewToResize]; [fakeTabBarImageView removeFromSuperview]; }]; hidden = NO; } } - (UIImage *)imageScreenshotFromView:(UIView *)aView { UIImage *viewImage; UIGraphicsBeginImageContextWithOptions(aView.bounds.size, aView.opaque, [[UIScreen mainScreen] scale]); [aView.layer renderInContext:UIGraphicsGetCurrentContext()]; viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return viewImage; } 

我尝试了几乎所有这些答案,但没有一个为我工作。 我的应用程序有一个UITabBarController作为根视图,每个选项卡都有一个UINavigationController。 其中一个UINavigationController具有一个UICollectionViewController作为顶部视图控制器。 当用户在UICollectionView中select一个项目时,我希望细节视图控制器被推到导航堆栈上。 我的详细视图然后在底部有一个工具栏。 我不希望工具栏出现在标签栏顶部,因为看起来很愚蠢,切换标签上下文将不需要从这个视图。 我可能很容易通过手动放置UIToolbars和UITabBars而不是使用UITabBarController和内置的UIToolbar来解决这个问题,但是这看起来像是太多的重构,而且有点不雅观。

最后,我的解决scheme非常简单:将UITabBarController的界限从屏幕底部延伸出来。 我将其添加到我的详细信息视图控制器:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Extend the UITabBarController to shift the tab bar off screen CGRect screenRect = [[UIScreen mainScreen] bounds]; CGRect tabBarControllerFrame = self.tabBarController.view.frame; if (animated) { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; tabBarControllerFrame.size.height = screenRect.size.height + self.tabBarController.tabBar.frame.size.height; [self.tabBarController.view setFrame:tabBarControllerFrame]; [UIView commitAnimations]; } else { tabBarControllerFrame.size.height = screenRect.size.height + self.tabBarController.tabBar.frame.size.height; [self.tabBarController.view setFrame:tabBarControllerFrame]; } // Now show the toolbar [self.navigationController setToolbarHidden:NO animated:animated]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; // Ensure the UITabBarController remains extended when subviews are laid out CGRect screenRect = [[UIScreen mainScreen] bounds]; CGRect tabBarControllerFrame = self.tabBarController.view.frame; tabBarControllerFrame.size.height = screenRect.size.height + self.tabBarController.tabBar.frame.size.height; [self.tabBarController.view setFrame:tabBarControllerFrame]; } 

然后,当用户popup回到我的UINavigationController的顶部时,重新显示标签栏,我将其添加到我的顶视图控制器:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Hide toolbar [self.navigationController setToolbarHidden:YES animated:animated]; // Tab bar back on to screen CGRect screenRect = [[UIScreen mainScreen] bounds]; CGRect tabBarControllerFrame = self.tabBarController.view.frame; if (tabBarControllerFrame.size.height != screenRect.size.height) { if (animated) { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; tabBarControllerFrame.size.height = screenRect.size.height; [self.tabBarController.view setFrame:tabBarControllerFrame]; [UIView commitAnimations]; } else { tabBarControllerFrame.size.height = screenRect.size.height; [self.tabBarController.view setFrame:tabBarControllerFrame]; } } } 

在iOS8中,只需设置tabBarhidden属性就足够了
像在斯威夫特,你可以

 rootTabVC = UITabBarController() rootTabVC?.tabBar.hidden = true 

我在appdelegate didFinishLaunchingWithOptions中这样做,它工作正常,我想如果我没有记错在旧的iOS版本中,您还需要将tabBarframe设置为屏幕外的东西,否则tabbar将不会显示,但它会仍占据着空间。

自IOS 7.1以来, “Swift”解决scheme:

 self.tabBarController?.tabBar.hidden = true // hide tabbar self.tabBarController?.tabBar.hidden = false // show tabbar 

希望这可以帮助!

Swift和@Saurabh代码的修改版本

方法

 func setTabBarHidden (bool:Bool){ for view in tabBarController!.view.subviews { if (view.isKindOfClass(UITabBar)){ let tabBar = view as! UITabBar UIView.animateWithDuration(0.3, animations: { () -> Void in var offset = CGFloat(50) if (bool == false){ offset = -50; } tabBar.frame = CGRect(origin: CGPointMake(tabBar.frame.origin.x, tabBar.frame.origin.y + offset), size: tabBar.frame.size) }) } } } 

以显示

 override func viewDidLoad() { setTabBarHidden(true) } 

隐藏

 override func viewWillDisappear(animated: Bool) { setTabBarHidden(false) } 

这里有一个简单的@Thomas Verbeek的版本,适合那些没有表格的VC(在iOS 8.4下testing):

 extension UITabBarController { /** Shows or hides the tabbar :param: hidden whether to show or hide the tabbar :param: animationDuration the animation's duration */ func setHidden(hidden:Bool, animationDuration:NSTimeInterval = 0.25) { let screenRect = UIScreen.mainScreen().bounds var fHeight = screenRect.size.height if !hidden { fHeight -= self.tabBar.frame.size.height } UIView.animateWithDuration(animationDuration, animations: { for view in self.view.subviews as! [UIView] { if view is UITabBar { view.frame = CGRectMake( view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height) } } }) } } 

而在这里,更直接的端口(未经testing):

 extension UITabBarController { /** Shows or hides the tabbar :param: hidden whether to show or hide the tabbar :param: animationDuration the animation's duration */ func setHidden(hidden:Bool, animationDuration:NSTimeInterval = 0.25) { let screenRect = UIScreen.mainScreen().bounds var fHeight = screenRect.size.height if UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) { fHeight = screenRect.size.width } if !hidden { fHeight -= self.tabBar.frame.size.height } UIView.animateWithDuration(animationDuration, animations: { for view in self.view.subviews as! [UIView] { if view is UITabBar { view.frame = CGRectMake( view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height) } else if hidden { view.frame = CGRectMake( view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight) } } }, completion: { finished in if !hidden { UIView.animateWithDuration(animationDuration, animations: { for view in self.view.subviews as! [UIView] { if !(view is UITabBar) { view.frame = CGRectMake( view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight) } } }) } }) } }