【问题标题】:change inactive color of the tab item更改选项卡项的非活动颜色
【发布时间】:2016-04-12 19:10:01
【问题描述】:

我可以使用 this answer 更改标签栏的活动颜色

我的问题是我不知道如何更改不活动的朋友和搜索选项卡。我想将其更改为红色。 我进行了很多搜索并决定询问,因为我找不到任何解决方案。请多多包涵,因为我是使用 swift 进行 ios 开发的新手。

【问题讨论】:

标签: ios iphone swift uiviewcontroller


【解决方案1】:

我将发布我所做的事情以实现我想要实现的目标。这也可能对将来像我这样的大人物有所帮助。

首先我为每个图标创建了两个图像

主页图标、好友图标和搜索图标各有白色和绿色

然后点击Assets.xcassets文件添加New Image Set

确保绿色图标 Render As 设置为 Original Image 因为这将是我们标签栏的非活动颜色图标 strong>.这是示例属性的示例屏幕截图

通过设置 Home Item 标签栏 Icon。请注意

System Item => Custom
Selected Image => Home_w//Name of the home white icon
Title => Home
Image=> Home_g//Name of the Inactive tabbar item in my case are sets to green icons

最终覆盖 AppDelegate.swift 中标签栏项目的标题颜色。请注意,我已将 UIColor_Hex_Swift 用于十六进制 UIColor。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let color:UIColor =  UIColor(rgba: "#026a34") //UIColor(hexString:"026a34")

    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: color], forState: .Normal)

    // then if StateSelected should be different, you should add this code
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: .Selected)

    return true
}

输出(白色的是选中的图片,绿色的是非活动图片)

【讨论】:

    【解决方案2】:

    从 iOS 10 开始,您可以使用它来更改选项卡项目的颜色(您必须将图像呈现为模板):

        UITabBar.appearance().tintColor = .blue
        UITabBar.appearance().unselectedItemTintColor = .gray
    

    【讨论】:

      【解决方案3】:

      您可以直接在资产目录中设置标签栏图像的属性Render As。您可以选择将属性设置为DefaultOriginal ImageTemplate Image

      【讨论】:

      • 在哪里可以找到资产目录?我真的不知道,因为我是 ios 开发新手
      【解决方案4】:

      试试这个

       self.tabBar.tintColor=[UIColor whiteColor];
      

      【讨论】:

      • 你能解释更多吗?我将把代码放在哪里?我知道我可以在不使用任何代码的情况下更改 tintcolor。我的问题是不活动的、搜索和朋友选项卡项。
      • 子类 UITabBarController 设置类。并写入 viewDidLoad
      【解决方案5】:

      this 可能是您希望搜索具体解决方案的最佳链接。

      【讨论】:

        猜你喜欢
        • 2016-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-01
        • 2022-01-20
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        相关资源
        最近更新 更多