0

I want to create Tab bar with dynamic tab. I have created tab bar with dynamic tabs but when I navigate to details screen from my tab bar screen there is black space in that details view controller. please suggest me the right way to implement it. Here is my code.

func creatTabBarProgrammatically(bottomMenuArray:[Any]) {
    DispatchQueue.main.async {
        self.navigationController?.setNavigationBarHidden(true, animated: false)

        let tabBarController = UITabBarController()
        tabBarController.tabBar.backgroundImage = self.getImageWithColor(UIColor.white, size: CGSize(width: self.view.frame.size.width, height: 49))
        tabBarController.hidesBottomBarWhenPushed = true

        let firstViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

        let ordersViewController = self.storyboard?.instantiateViewController(withIdentifier: "OrdersViewController") as! OrdersViewController

        let myProfileViewController = self.storyboard?.instantiateViewController(withIdentifier: "MyProfileViewController") as! MyProfileViewController


        var controllers = [Any]()
        controllers.append(firstViewController)

        for i in 0 ..< bottomMenuArray.count{
            if let dict = bottomMenuArray[i] as? [String:Any]{
                if let name = dict["name"] as? String{
                    if name == "orders"{
                        controllers.append(ordersViewController)
                    }
                    else if name == "profile"{
                        controllers.append(myProfileViewController)
                    }else{

                    }
                }
            }
        }

        tabBarController.viewControllers = controllers as? [UIViewController]

        // Setting Title selected image and unselected image to tab
        for i in 0 ..< bottomMenuArray.count{
            if let dict = bottomMenuArray[i] as? [String:Any]{
                if let name = dict["name"] as? String{
                    if name == "orders"{
                        let tabBarItem :UITabBarItem = tabBarController.tabBar.items![i + 1]
                        tabBarItem.title = "Orders"
                        tabBarItem.selectedImage = UIImage(named: "tabordersselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
                        tabBarItem.image = UIImage(named: "tabordersunselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
                    }
                    else if name == "profile"{
                        let tabBarItem :UITabBarItem = tabBarController.tabBar.items![i + 1]
                        tabBarItem.title = "Profile"
                        tabBarItem.selectedImage = UIImage(named: "tabprofileselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
                        tabBarItem.image = UIImage(named: "tabprofileunselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
                    }else{

                    }
                }
            }
        }

        let tabBarItem1:UITabBarItem = tabBarController.tabBar.items![0]
        tabBarItem1.title = "Home"
        tabBarItem1.selectedImage = UIImage(named: "tabhomeselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        tabBarItem1.image = UIImage(named: "tabhomeunseleted")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)

        tabBarController.viewControllers = controllers.map { UINavigationController(rootViewController: $0 as! UIViewController)}
        self.navigationController!.pushViewController(tabBarController, animated: false)
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jayesh Joshi
  • 37
  • 1
  • 9

1 Answers1

0

Try to force layout on controller you got bottom black bar

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.view.setNeedsLayout()
    }
SuryaKantSharma
  • 1,113
  • 12
  • 27