当使用hidesBottomBarWhenPushed时,我希望当我按下另一个视图时,标签栏会重新出现

时间:2011-05-24 22:36:17

标签: iphone objective-c uinavigationcontroller uitabbar

我有一个导航控制器。对于其中一个视图,我想隐藏底部标签栏,以便获得最大可能的屏幕空间。要做到这一点,我有:

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

但是对于我推动堆栈的下一个视图,我希望标签栏重新出现。有没有办法做到这一点?

6 个答案:

答案 0 :(得分:12)

从iOS5开始,有一种非常简单的方法可以实现这一目标。它与Deepak的方法基本相同,但动画中没有任何工件 - 一切看起来都符合预期。

在init上,设置

self.hidesBottomBarWhenPushed = YES;
就像你上面一样。当需要在堆栈上推送新控制器时,它就像:

一样简单
self.hidesBottomBarWhenPushed = NO;

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

self.hidesBottomBarWhenPushed = YES;

在按下控制器后将值重置为YES非常重要,以便在用户点击“返回”按钮并重新查看视图时重新隐藏栏。

答案 1 :(得分:6)

我已经解决了这个问题:

几乎所有的ViewControllers都是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?
}

在ViewController中覆盖变量“prefersBottomBarHidden”,其中应隐藏BottomBar:

override var prefersBottomBarHidden: Bool? { return true }

答案 2 :(得分:3)

案例一: 要在一个cetain UIVIewController中隐藏UITabbarController,例如在调用self.performSegueWithIdentifier("Identifier", sender: self)时,必须先设置self.hidesBottomBarWhenPushed = true标志。在self.hidesBottomBarWhenPushed = false标志之后。但我们必须通过一个UIViewController来强调,UITabbarController将重新出现,如果你需要将UITabbarController与单个UIViewControler一起使用,它就不会产生正确的结果。

  在FirstItemViewController中

    @IBAction func pushToControllerAction(sender: AnyObject) {
        self.hidesBottomBarWhenPushed = true
        self.performSegueWithIdentifier("nextController", sender: self)
        self.hidesBottomBarWhenPushed = false
    }

enter image description here

案例二: 要在某个UIVIewController中隐藏UITabbarController,之后应该弹出UITabbarController,例如,在调用self.performSegueWithIdentifier("nextController", sender: self)时,必须在方法之前设置self.hidesBottomBarWhenPushed = true。应该按照代码示例中的说明配置方法中的willMoveToParentViewController(parent: UIViewController?)

  

在第一个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
                }

enter image description here

  

<强> Test project

答案 3 :(得分:2)

可以重新出现,但会导致动画不正确。页面左侧,右侧栏右侧。所以这可能不是你想要的行为。但是在同一个控制器中,在推入下一个视图控制器之前执行self.hidesBottomBarWhenPushed = NO;

答案 4 :(得分:1)

在根视图控制器“A”(显示tabBar)中,当需要显示另一个视图控制器“B”时,不需要tabBar:

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

答案 5 :(得分:1)

自问这个问题以来已经有一段时间了,但这些答案都没有解决使用故事板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 }

    }
}