When using hidesBottomBarWhenPushed, i want the tab bar to reappear when i push another view

前端 未结 6 1178
南方客
南方客 2020-12-06 11:09

I have a navigation controller. For one of the views i want to hide the bottom tab bar, so it gets the max possible screen real estate. To do this, i have:

-         


        
相关标签:
6条回答
  • 2020-12-06 11:42

    I'm have solved this problem like that:

    Almost all my ViewControllers are children of BaseViewController.

    So, example:

    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?
    }
    

    Just override variable "prefersBottomBarHidden" in ViewController where BottomBar should be hidden:

    override var prefersBottomBarHidden: Bool? { return true }
    
    0 讨论(0)
  • 2020-12-06 11:42

    It's been a while since this question was asked, but none of these answers address using Storyboard segues. It turns out to be pretty easy:

    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 }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-06 11:43

    Case one: To hide UITabbarController in a cetain UIVIewController, for example while calling self.performSegueWithIdentifier("Identifier", sender: self), it is necesssary prior to to that, set self.hidesBottomBarWhenPushed = true flag. And after self.hidesBottomBarWhenPushed = false flag. But we have to understad that through one UIViewController, UITabbarController will re-appear and, in case if you need to use UITabbarController with single UIViewControler, it wont yield right result.

    in the FirstItemViewController

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

    Case Two: To hide UITabbarController in a certain UIVIewController, after which a UITabbarController should be popped, it is necessary, for example, while calling self.performSegueWithIdentifier("nextController", sender: self) , to set self.hidesBottomBarWhenPushed = true before the method. Alse willMoveToParentViewController(parent: UIViewController?) in the method should be configured as it shown in the code example.

    in the first UIViewController "FirstItemViewController"

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

    in the next 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 code:

    let viewControllers = self.navigationController!.viewControllers
                    if ((viewControllers[viewControllers.count - 2]) is (FirstItemViewController)) {
                        (viewControllers[viewControllers.count - 2] as! FirstItemViewController).hidesBottomBarWhenPushed = false
                    }
    

    Test project

    0 讨论(0)
  • 2020-12-06 11:53

    In a root view controller "A" (which is showing the tabBar), when it comes time to show another view controller "B" where no tabBar is wanted:

    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
    

    In view controller B, when it comes time to show a third view controller C (tabBar wanted again):

    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
    
    0 讨论(0)
  • 2020-12-06 11:55

    As of iOS5, there's a very easy means of accomplishing this. It's essentially the same method as Deepak, but there aren't any artifacts with the animation - everything looks as expected.

    On init, set

    self.hidesBottomBarWhenPushed = YES;
    

    just as you have above. When it's time to push the new controller on the stack, it's as simple as:

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

    It's important to reset the value to YES after the controller has been pushed in order to re-hide the bar when the user taps the Back button and the view comes back into view.

    0 讨论(0)
  • 2020-12-06 12:05

    One can make it reappear but it will result in an incorrect animation. Page comes in left and the bottom bar right. So it is probably not the behavior you want. But in the same controller, do self.hidesBottomBarWhenPushed = NO; before pushing the next view controller in.

    0 讨论(0)
提交回复
热议问题