Hiding the tabbar and removing the space

前端 未结 14 1692
生来不讨喜
生来不讨喜 2020-11-30 00:10

Is there a way to hide tabbar and remove that space left (around 50px) ?

I tried

self.tabBarController?.tabBar.hidden = true
self.extendedLayoutIncl         


        
14条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 01:01

    My preferred way to do that is using a wrapping controller. If I want to hide the tab bar, I just increase the height of the tab bar controller, thus effectively the tab bar is moved out of the screen.

    With this solution you don't need to hack tab bar frame and you don't depend on navigation controller push animation:

    import UIKit
    
    class ViewController: UIViewController {
        let tabController: UITabBarController = {
            let tabController = UITabBarController()
            // setup your tabbar controller here
    
            return tabController;
        }()
    
        var tabbarHidden = false {
            didSet {
                var frame = self.view.bounds;
    
                if (tabbarHidden) {
                    frame.size.height += self.tabController.tabBar.bounds.size.height;
                }
    
                self.tabController.view.frame = frame;
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // add the tab controller as child controller
            addChildViewController(self.tabController)
            self.tabController.view.frame = self.view.bounds
            self.tabController.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
            self.view.addSubview(self.tabController.view)
            self.tabController.didMoveToParentViewController(self)
    
            // for debugging
            let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(switchTabbar))
            self.tabController.view.addGestureRecognizer(tapRecognizer)
        }
    
        override func childViewControllerForStatusBarStyle() -> UIViewController? {
            return self.tabController
        }
    
        override func childViewControllerForStatusBarHidden() -> UIViewController? {
            return self.tabController
        }
    
        func switchTabbar() {
            UIView.animateWithDuration(0.3) {
                self.tabbarHidden = !self.tabbarHidden
            }
        }
    }
    

提交回复
热议问题