[已解决]swift设置Tab的背景色

 想要设置tab的背景色:
tab here want to add background color
相关已有代码为:
        //6. create tabs
        let viewConversation    = ConversationViewController()
        let viewContact         = ContactViewController()
        let viewFile            = FileViewController()
        let viewFavorite        = FavoriteViewController()
        let viewMy              = MyViewController()

        let nvcConversation = UINavigationController(rootViewController: viewConversation)
        nvcConversation.tabBarItem = UITabBarItem(title: mainTabs[0], image: UIImage(named:"tab_conversation"), tag:0)
        nvcConversation.tabBarItem.selectedImage = UIImage(named: "tab_conversation_selected")
        //nvcConversation.tabBarItem.badgeValue = "3"

        let nvcContact = UINavigationController(rootViewController: viewContact)
        nvcContact.tabBarItem = UITabBarItem(title: mainTabs[1], image: UIImage(named:"tab_contact"), tag:1)
        nvcContact.tabBarItem.selectedImage = UIImage(named: "tab_contact_selected")

        let nvcFile = UINavigationController(rootViewController: viewFile)
        nvcFile.tabBarItem = UITabBarItem(title: mainTabs[2], image: UIImage(named:"tab_file"), tag:2)
        nvcFile.tabBarItem.selectedImage = UIImage(named: "tab_file_selected")

        let nvcFavorite = UINavigationController(rootViewController: viewFavorite)
        nvcFavorite.tabBarItem = UITabBarItem(title: mainTabs[3], image: UIImage(named:"tab_favorite"), tag:3)
        nvcFavorite.tabBarItem.selectedImage = UIImage(named: "tab_favorite_selected")

        let nvcMy = UINavigationController(rootViewController: viewMy)
        nvcMy.tabBarItem = UITabBarItem(title: mainTabs[4], image: UIImage(named:"tab_my"), tag:4)
        nvcMy.tabBarItem.selectedImage = UIImage(named: "tab_my_selected")

        self.viewControllers = [nvcConversation, nvcContact, nvcFile, nvcFavorite, nvcMy]

        //set bar item font
        let barItemFont = FontMainTab
        for nvcController in self.viewControllers!{
            nvcController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: barItemFont], forState: UIControlState.Normal)
        }

[解决过程]

1.搜:

swift tab background color

参考:

试了试:
        UITabBar.appearance().tintColor = UIColor.purpleColor()
 效果:
tab add tint color for single tab
不是所要的。
       UITabBar.appearance().tintColor = UIColor.purpleColor()
        UITabBar.appearance().barTintColor = UIColor.yellowColor()
 效果:
set uibar barTintColor is look good
达到所需要的效果了。
再去调节:
let ColorMainTabBackground:UIColor = UIColor(hexString: "#f6f6f6")!

        //UITabBar.appearance().tintColor = UIColor.purpleColor()
        UITabBar.appearance().barTintColor = ColorMainTabBackground

最终的效果:

final tab background color for jiandao

官网的解释是:
 xcode note for uibar tintcolor
[总结]
想要设置tab的背景色,实际上是:
设置UITabBar的背景色,对应设置代码为:
UITabBar.appearance().barTintColor = UIColor.yellowColor()
即可。


发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量