最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Swift crifan 3070浏览 0评论
 想要设置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()
即可。

转载请注明:在路上 » [已解决]swift设置Tab的背景色

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.189 seconds, using 22.05MB memory