选择标签栏按钮时无法更改我的色泽颜色

时间:2019-09-23 11:25:05

标签: ios swift

我是新手,请有人帮我吗?

我尝试在应用程序委托中添加外观tintcolor,但我的构想已耗尽,这是行不通的

let myTabBar = UITabBar()

myTabBar.tintColor = UIColor.green

if #available(iOS 10.0, *) {
   myTabBar.unselectedItemTintColor = UIColor.red
}

let one = UITabBarItem()
    one.title = "Home"
    one.tag = 1
    one.image = UIImage(named: "home")

    myTabBar.delegate = self

 func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    switch item.tag {
    case 1:
        let controller = businessStoryboard.instantiateViewController(withIdentifier: "BusinessListViewController")
        addChildViewController(controller)
        view.addSubview((controller.view)!)
        controller.didMove(toParentViewController: self)
        break
    default:
        break
    }
}

1 个答案:

答案 0 :(得分:0)

您需要从UITabBarController获取tabBar,然后更改tint属性,类似这样,假设您的rootViewController是UITabBarController

if let tabBarController = self.window?.rootViewController as? UITabBarController {
        self.configureTabBar(tabBarController:tabBarController)
    }

 func configureTabBar(tabBarController: UITabBarController) {
            // Tab bar customization
        tabBarController.tabBar.tintColor =  UIColor.red
    }