如何以编程方式创建带有动态标签的标签栏?底部黑屏问题

时间:2018-09-01 11:26:13

标签: ios swift uikit uitabbarcontroller uitabbar

我想创建带有动态标签的标签栏。我已经创建了带有动态标签的标签栏,但是当我从标签栏屏幕导航到详细信息屏幕时,该详细信息视图控制器中有空白。请建议我正确的实施方式。这是我的代码。

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

1 个答案:

答案 0 :(得分:0)

尝试在控制器上强制设置底部黑条

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.view.setNeedsLayout()
    }