gpt4 book ai didi

ios - swift UITabbaritem 颜色

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:02 25 4
gpt4 key购买 nike

我想知道如何为我的 tabBar 使用我想要的颜色。

我知道如何更改背景,我也知道如何更改 tabbar.item 颜色和文本,但我不明白如何:

  • 未选中标签栏项的默认灰色
  • 如果项目被选中则更改颜色(我使用的渲染模式始终是原始的,因为我找不到另一种方法来从未选中的选项卡栏项目中删除默认灰色)

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {

    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

    tabBarItem.title = "test"
    tabBarItem.image = UIImage(named: "first.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

    }

如何在我想要的状态下使用我想要的颜色?

最佳答案

UITabBar 有一个 tintColor 属性,但这会设置所选图像的色调,而不是未选图像的色调。您正在正确设置未选择的图像 AFAIK。要更改所选图像的颜色,您可以使用 UITabBar 上的 tintColor(如果您希望所有图像都具有相同的色调),或设置您的 UITabBarItemselectedImage 渲染模式为 AlwaysOriginal

tabBarItem.selectedImage = UIImage(named: "first-selected")!.imageWithRenderingMode(.AlwaysOriginal)

我已将 UIImage 设置为未包装的可选,因为您可能希望它在没有图像文件时崩溃。这将有助于确保您的图像真正被加载,而不是默默地失败:-)

您可能还想设置标签的颜色,否则您的文本将与您的图像颜色不匹配。下面设置了所有 UITabBarItem 的默认值,但您可以在每个项目的基础上设置(或覆盖)它。

UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blueColor()}, forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.redColor()}, forState:.Normal)

关于ios - swift UITabbaritem 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26704896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com