当使用hidesBottomBarWhenPushed时,我希望当我按另一种视图时重新显示选项卡。

我有一个导航控制器。对于其中一个视图,我想隐藏底部的标签栏,以便获得最大的屏幕空间。为此,我有:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES; // To hide the tab bar
    }
    return self;
}
但是对于我要推入堆栈的下一个视图,我希望标签栏重新出现。有没有办法做到这一点?     
已邀请:
可以使其重新出现,但会导致动画不正确。页面位于左侧,底部栏位于右侧。因此,这可能不是您想要的行为。但是在同一控制器中,在推入下一个视图控制器之前,请执行
self.hidesBottomBarWhenPushed = NO;
。     
从iOS5开始,有一种非常简单的方法可以完成此操作。基本上与Deepak的方法相同,但是动画没有任何伪像-一切都按预期进行。 初始化时,设置
self.hidesBottomBarWhenPushed = YES;
就像上面一样是时候将新控制器推入堆栈了,它很简单:
self.hidesBottomBarWhenPushed = NO;

UIViewController *controller = [[[BBListingController alloc] init] autorelease];
[self.navigationController pushViewController:controller];

self.hidesBottomBarWhenPushed = YES;
重要的是,在按下控制器后将值重置为YES,以便在用户点击“后退”按钮并且视图返回视图时重新隐藏该栏。     
我已经解决了这样的问题: 我几乎所有的ViewController都是BaseViewController的子级。 因此,例如:
class BaseVC: UIViewController {
    final override var hidesBottomBarWhenPushed: Bool {
        get {
            if navigationController?.viewControllers.last == self {
                return prefersBottomBarHidden ?? super.hidesBottomBarWhenPushed
            } else {
                return false
            }
        } set {
            super.hidesBottomBarWhenPushed = newValue
        }
   }
   private(set) var prefersBottomBarHidden: Bool?
}
只需在应该隐藏BottomBar的ViewController中重写变量\“ prefersBottomBarHidden \”:
override var prefersBottomBarHidden: Bool? { return true }
    
情况一: 要将UITabbarController隐藏在某些UIVIewController中,例如在调用
self.performSegueWithIdentifier(\"Identifier\", sender: self)
时,必须在此之前设置set7ѭ标志。在after8ѭ标志之后。但是我们必须低估,通过一个UIViewController,UITabbarController将重新出现,并且如果您需要将UITabbarController与单个UIViewControler一起使用,它将不会产生正确的结果。   在FirstItemViewController中
    @IBAction func pushToControllerAction(sender: AnyObject) {
        self.hidesBottomBarWhenPushed = true
        self.performSegueWithIdentifier(\"nextController\", sender: self)
        self.hidesBottomBarWhenPushed = false
    }
案例二: 要将UITabbarController隐藏在某个UIVIewController中,然后应弹出UITabbarController,例如,有必要在调用
self.performSegueWithIdentifier(\"nextController\", sender: self)
时在方法前设置
self.hidesBottomBarWhenPushed = true
。该方法中的其他“ 12”应按代码示例所示进行配置。   在第一个UIViewController \“ FirstItemViewController \”中
 @IBAction func pushToControllerAction(sender: AnyObject) {
     self.hidesBottomBarWhenPushed = true
     self.performSegueWithIdentifier(\"nextController\", sender: self)
 }
     在下一个UIViewController \“ ExampleViewController \”`中
 override func willMoveToParentViewController(parent: UIViewController?) {
         if parent == nil {
             var viewControllers = self.navigationController!.viewControllers
             if ((viewControllers[viewControllers.count - 2]).isKindOfClass(FirstItemViewController.self)) {
                 (viewControllers[viewControllers.count - 2] as! FirstItemViewController).hidesBottomBarWhenPushed = false
             }
         }
 }
Swift 3代码:
let viewControllers = self.navigationController!.viewControllers
                if ((viewControllers[viewControllers.count - 2]) is (FirstItemViewController)) {
                    (viewControllers[viewControllers.count - 2] as! FirstItemViewController).hidesBottomBarWhenPushed = false
                }
  测试项目     
自问这个问题已经有一段时间了,但是这些答案都没有使用Storyboard segues解决。事实证明这很简单:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == \"MyViewControllerIdentifier\" {

        // Hide the tabbar during this segue
        hidesBottomBarWhenPushed = true

        // Restore the tabbar when it\'s popped in the future
        DispatchQueue.main.async { self.hidesBottomBarWhenPushed = false }

    }
}
    
在根视图控制器\“ A \”(正在显示tabBar)中,当需要显示另一个不需要tabBar的视图控制器\“ B \”时:
self.hidesBottomBarWhenPushed = YES; // hide the tabBar when pushing B
[self.navigationController pushViewController:viewController_B animated:YES];
self.hidesBottomBarWhenPushed = NO; // for when coming Back to A
在视图控制器B中,当需要显示第三个视图控制器C时(再次需要tabBar):
self.hidesBottomBarWhenPushed = NO; // show the tabbar when pushing C
[self.navigationController pushViewController:viewController_C animated:YES];
self.hidesBottomBarWhenPushed = YES; // for when coming Back to B
    

要回复问题请先登录注册