如何隐藏在iOS中的animation标签栏?

所以我有一个连接到IBAction的button。 当我按下button时,我想用animation隐藏iOS应用程序中的标签栏。 这个[self setTabBarHidden:hidden animated:NO]; 或者这个[self.tabBarController setTabBarHidden:hidden animated:YES]; 不起作用。 这是我的代码没有animation:

 - (IBAction)picture1:(id)sender { [self.tabBarController.tabBar setHidden:YES]; } 

任何帮助将不胜感激:D

我尝试使用以下公式来保留视图animation:

 // pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion - (void)setTabBarVisible:(BOOL)visible animated:(BOOL)animated completion:(void (^)(BOOL))completion { // bail if the current state matches the desired state if ([self tabBarIsVisible] == visible) return (completion)? completion(YES) : nil; // get a frame calculation ready CGRect frame = self.tabBarController.tabBar.frame; CGFloat height = frame.size.height; CGFloat offsetY = (visible)? -height : height; // zero duration means no animation CGFloat duration = (animated)? 0.3 : 0.0; [UIView animateWithDuration:duration animations:^{ self.tabBarController.tabBar.frame = CGRectOffset(frame, 0, offsetY); } completion:completion]; } //Getter to know the current state - (BOOL)tabBarIsVisible { return self.tabBarController.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame); } //An illustration of a call to toggle current state - (IBAction)pressedButton:(id)sender { [self setTabBarVisible:![self tabBarIsVisible] animated:YES completion:^(BOOL finished) { NSLog(@"finished"); }]; } 

在处理故事板时,可以轻松设置视图控制器以在推送时隐藏选项卡栏,在目标视图控制器上select此checkbox:
在这里输入图像说明

根据Apple文档,UIViewController的hidesBottomBarWhenPushed属性是一个布尔值,指示当视图控制器被推到导航控制器上时,屏幕底部的工具栏是否隐藏。

最上面的视图控制器上的这个属性的值确定工具栏是否可见。

build议隐藏标签栏的方法如下

  ViewController *viewController = [[ViewController alloc] init]; viewController.hidesBottomBarWhenPushed = YES; // This property needs to be set before pushing viewController to the navigationController's stack. [self.navigationController pushViewController:viewController animated:YES]; 

但是,请注意,这种方法只适用于相应的viewController,并且不会传播到其他视图控制器,除非在将其推送到导航控制器的堆栈之前,在其他viewControllers中开始设置相同的hidesBottomBarWhenPushed属性。

Swift版本:

 @IBAction func tap(sender: AnyObject) { setTabBarVisible(!tabBarIsVisible(), animated: true, completion: {_ in }) } // pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion func setTabBarVisible(visible: Bool, animated: Bool, completion:(Bool)->Void) { // bail if the current state matches the desired state if (tabBarIsVisible() == visible) { return completion(true) } // get a frame calculation ready let height = tabBarController!.tabBar.frame.size.height let offsetY = (visible ? -height : height) // zero duration means no animation let duration = (animated ? 0.3 : 0.0) UIView.animateWithDuration(duration, animations: { let frame = self.tabBarController!.tabBar.frame self.tabBarController!.tabBar.frame = CGRectOffset(frame, 0, offsetY); }, completion:completion) } func tabBarIsVisible() -> Bool { return tabBarController!.tabBar.frame.origin.y < CGRectGetMaxY(view.frame) } 

Swift 3.0版本,使用扩展名:

 extension UITabBarController { private struct AssociatedKeys { // Declare a global var to produce a unique address as the assoc object handle static var orgFrameView: UInt8 = 0 static var movedFrameView: UInt8 = 1 } var orgFrameView:CGRect? { get { return objc_getAssociatedObject(self, &AssociatedKeys.orgFrameView) as? CGRect } set { objc_setAssociatedObject(self, &AssociatedKeys.orgFrameView, newValue, .OBJC_ASSOCIATION_COPY) } } var movedFrameView:CGRect? { get { return objc_getAssociatedObject(self, &AssociatedKeys.movedFrameView) as? CGRect } set { objc_setAssociatedObject(self, &AssociatedKeys.movedFrameView, newValue, .OBJC_ASSOCIATION_COPY) } } override open func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() if let movedFrameView = movedFrameView { view.frame = movedFrameView } } func setTabBarVisible(visible:Bool, animated:Bool) { //since iOS11 we have to set the background colour to the bar color it seams the navbar seams to get smaller during animation; this visually hides the top empty space... view.backgroundColor = self.tabBar.barTintColor // bail if the current state matches the desired state if (tabBarIsVisible() == visible) { return } //we should show it if visible { tabBar.isHidden = false UIView.animate(withDuration: animated ? 0.3 : 0.0) { //restore form or frames self.view.frame = self.orgFrameView! //errase the stored locations so that... self.orgFrameView = nil self.movedFrameView = nil //...the layoutIfNeeded() does not move them again! self.view.layoutIfNeeded() } } //we should hide it else { //safe org positions orgFrameView = view.frame // get a frame calculation ready let offsetY = self.tabBar.frame.size.height movedFrameView = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height + offsetY) //animate UIView.animate(withDuration: animated ? 0.3 : 0.0, animations: { self.view.frame = self.movedFrameView! self.view.layoutIfNeeded() }) { (_) in self.tabBar.isHidden = true } } } func tabBarIsVisible() ->Bool { return orgFrameView == nil } } 
  • 这是基于Sherwin Zadeh在玩了几个小时后的input。
  • 而不是移动tabbar本身,它移动视图的框架,这有效地滑动tabbar很好地从屏幕的底部,但…
  • …具有的优点是,UITabbar控制器内部显示的内容也将全屏显示!
  • 注意它也使用AssociatedObjectfunction来附加数据到UIView没有子类化,因此扩展是可能的(扩展不允许存储的属性)

在这里输入图像说明

尝试在animation中设置tabBar的框架。 看到这个教程。

只是要知道,这是不好的做法,你应该设置显示/隐藏TabBar UIViewController推动通过设置属性hidesBottomBarWhenPushedYES

在3.0 / iOS10 / Xcode 8中试过:

  self.tabBarController?.tabBar.isHidden = true 

当我的控制器显示时,我将其设置:(和导航后隐藏时,

 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.tabBarController?.tabBar.isHidden = false } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.tabBarController?.tabBar.isHidden = true } 

顺便说一句:最好有一个标志来保存,如果显示或不,因为其他通风口可以最终触发隐藏/显示

不幸的是,我不能评论HixField的答案,因为我没有足够的声誉,所以我必须离开这个作为一个单独的答案。

他的答案是遗漏了movedFrameView的计算属性,它是:

 var movedFrameView:CGRect? { get { return objc_getAssociatedObject(self, &AssociatedKeys.movedFrameView) as? CGRect } set { objc_setAssociatedObject(self, &AssociatedKeys.movedFrameView, newValue, .OBJC_ASSOCIATION_COPY) } } 

在Swift 4中重写了Sherwin Zadeh的回答:

 /* tab bar hide/show animation */ extension AlbumViewController { // pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion func setTabBarVisible(visible: Bool, animated: Bool, completion: ((Bool)->Void)? = nil ) { // bail if the current state matches the desired state if (tabBarIsVisible() == visible) { if let completion = completion { return completion(true) } else { return } } // get a frame calculation ready let height = tabBarController!.tabBar.frame.size.height let offsetY = (visible ? -height : height) // zero duration means no animation let duration = (animated ? kFullScreenAnimationTime : 0.0) UIView.animate(withDuration: duration, animations: { let frame = self.tabBarController!.tabBar.frame self.tabBarController!.tabBar.frame = frame.offsetBy(dx: 0, dy: offsetY) }, completion:completion) } func tabBarIsVisible() -> Bool { return tabBarController!.tabBar.frame.origin.y < view.frame.maxY } } 

这对我来说: [self.tabBar setHidden:YES];
self是视图控制器,tabBar是tabBar的ID。