Search code examples
swiftuikituitabbarcontroller

How to display bottom tab of tab bar controller project wide?


class func tabBarController () -> UINavigationController{
    
    let tabBarController = UITabBarController()
    
    let homeVC = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "HomeVC")
    
    let wishlistVC = UIStoryboard(name: "Wishlist", bundle: nil).instantiateViewController(withIdentifier: "WishlistVC")
    
    homeVC.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "home"), selectedImage: UIImage(named: "home"))
    
    wishlistVC.tabBarItem = UITabBarItem(title: "Wishlist", image: UIImage(named: "wishlist"), selectedImage: UIImage(named: "wishlist"))
    
    tabBarController.tabBar.tintColor = .black
    tabBarController.tabBar.isTranslucent = false
    tabBarController.tabBar.barTintColor = UIColor.white
    tabBarController.tabBar.layer.shadowOffset = CGSize(width: 0, height: 0.3)
    tabBarController.tabBar.layer.shadowRadius = 4.0
    tabBarController.tabBar.layer.shadowColor = UIColor.gray.cgColor
    tabBarController.tabBar.layer.shadowOpacity = 0.2
    tabBarController.tabBar.clipsToBounds = false
    tabBarController.tabBar.backgroundImage = UIImage()
    tabBarController.tabBar.shadowImage = UIImage()
    tabBarController.hidesBottomBarWhenPushed = true

    let tabBarViewController = [homeVC, wishlistVC]
    
    let navigationController = UINavigationController.init(rootViewController: tabBarController)
    navigationController.navigationBar.isTranslucent = true
    navigationController.navigationBar.shadowImage = UIImage()
    navigationController.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
    navigationController.navigationBar.tintColor = UIColor.darkGray
    navigationController.view.backgroundColor = UIColor(named: "background_color")
    navigationController.navigationBar.barTintColor = .white
    let attributes = [NSAttributedString.Key.font: UIFont(name: "Roboto-Medium", size: 18)!]
    navigationController.navigationBar.titleTextAttributes = attributes
    
    return navigationController
}

This is my programmatic way to implement tab bar controller.

When another view controller is pushed then I want the bottom tab to be displayed on other screen as well.

** Please anyone help me ** Thanks in advance


Solution

  • You need to structure your view hierarchy like this

    - tabBarController
         - navigation 1
            - vc 1
         - navigation 2
            - vc 2
    

    the idea behind this is when you wrap the tabController inside a navigation and push a vc it takes the whole screen , while when you push a vc to a navigation that's inside a specific tab it persists the tab below as the navigation is inside the tab

    let navi1 = UINavigationController(rootViewController: homeVC)
    // configure  navi1
    .....
    .....
    let navi2 = UINavigationController(rootViewController: wishlistVC)
    // configure  navi2
    .....
    .....
    tabBarController.viewControllers = [navi1,navi2]