Search code examples
iosswiftxcodeuitabbarcontrollerxcode-storyboard

How to remove a tab bar from view having two tab bars on top of each other?


I want to remove the black tab bar from the A screen. I tried to do the code below to screen but it removes the white one.

override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarController?.tabBar.isHidden = true

    }

tab bars enter image description here


Solution

  • in my opinion this not a good user experience for iOS users, but any way please try the following approach and it will works fine with you

    1- first of all build try to build the following hierarchy in your stroyboard

    enter image description here

    note that the gray view Controller is not UITabBarController it just a UIViewController, but it contain a UITabBar object

    2- you need to give the red view controller stroyboard id "RedViewController" and also give the blue view controller "BlueViewController"

    3- give the nested UITabBarItem tag 0 for the first UITabBarItem which named "Favorite" and give tag 1 for second UITabBarItem which named "more"

    enter image description here 4- finally add the following class to the UIViewController that have the UITabBar object, in our example it will be the gray view controller

    class SecondViewController: UIViewController {
    
    lazy var blueViewController: UIViewController? = {
        self.storyboard?.instantiateViewController(withIdentifier: "BlueViewController")
    }()
    
    lazy var redViewController: UIViewController? = {
        self.storyboard?.instantiateViewController(withIdentifier: "RedViewController")
    }()
    
    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var nestedTabBar: UITabBar!
    override func viewDidLoad() {
        super.viewDidLoad()
        nestedTabBar.delegate = self
        addRedViewController()
        self.nestedTabBar.selectedItem = self.nestedTabBar.items?.first
    }
    
    func addBlueViewController() {
    
        if let redViewController = redViewController {
            redViewController.willMove(toParent: nil)
            redViewController.view.removeFromSuperview()
            redViewController.removeFromParent()
        }
    
        if let blueViewController = blueViewController {
            addChild(blueViewController)
            blueViewController.view.frame = containerView.frame
            containerView.addSubview(blueViewController.view)
            blueViewController.didMove(toParent: self)
        }
    }
    
    func addRedViewController() {
    
        if let blueViewController = blueViewController {
            blueViewController.willMove(toParent: nil)
            blueViewController.view.removeFromSuperview()
            blueViewController.removeFromParent()
        }
    
        if let redViewController = redViewController {
            addChild(redViewController)
            redViewController.view.frame = containerView.frame
            containerView.addSubview(redViewController.view)
            redViewController.didMove(toParent: self)
        }  
      }
    }
    
    extension SecondViewController: UITabBarDelegate {
    
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    
        if item.tag == 0 {
            addRedViewController()
        } else {
            addBlueViewController()
        }
      }
    }