在故事板中更改选定的选项卡栏项目颜色

201

我想在选中时将标签栏项目更改为粉色,而不是默认的蓝色。

如何使用Xcode 6中的故事板编辑器完成此操作?

以下是我的当前设置,但无效果。蓝色背景有效,但粉色无效:

图片描述


可能是 tab-bar-item-tint-color 的重复问题! - User31
请查看此答案,它将帮助您找到解决方案:https://stackoverflow.com/a/58727092/7804300 - Talha Ahmed
15个回答

5
一些情况下,我们无法仅通过Storyboard来更改选定的选项卡栏Tint颜色,因此我在我的ViewDidLoad中添加了以下代码,希望这能够帮到你。
[[UITabBar appearance] setTintColor:[UIColor whiteColor]]; 

5

在我的情况下,从storyboard中设置图像色调有效。

enter image description here


5
在你的应用委托(app delegate)中的did_finish_launching_with_options函数中添加以下代码:
UITabBar.appearance().tintColor = UIColor( red: CGFloat(255/255.0), green: CGFloat(99/255.0), blue: CGFloat(95/255.0), alpha: CGFloat(1.0) )

输入所需颜色的RGB值


4

以下是适用于iOS 10的Swift 3解决方案:

首先,创建自己的选项卡控制器子类,并将其添加到故事板中的选项卡控制器。在viewDidLoad()方法中,您可以自定义选项卡栏。需要说明的是,tabBartintColor属性代表所选项目的颜色,而不是未选项目的颜色!为了更改未选项目的颜色,建议循环遍历每个项目,并使用图像的原始颜色,以便它们不会自动呈现为灰色。

class CustomTabBarVC: UITabBarController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.tabBar.tintColor = AppColor.normalRed
        self.tabBar.barTintColor = .white
        self.tabBar.isTranslucent = true

        if let items = self.tabBar.items
        {
            for item in items
            {
                if let image = item.image
                {
                    item.image = image.withRenderingMode( .alwaysOriginal )
                }
            }
        }
    }
}

这种方法的唯一缺点是您的商品图片必须已经具有您想要的颜色。

复杂。Jarrod的答案适用于所有图标。 - Hedylove

2
你可以创建一个UITabBarController的子类,并在storyboard中将其替换为原有的控制器。 在你的子类的viewDidLoad方法中调用以下代码:
[self.tabBar setTintColor:[UIColor greenColor]];

出现错误:'UITabBar' 没有名为 'setSelectedImageTintColor' 的成员。 - Deekor
我正在使用Swift,所以我写了:self.tabBar.setSelectedImageTintColor = UIColor.greenColor,不确定是否正确。 - Deekor
2
@Deekor,应该使用tintColor而不是selectedImageTintColor,顺便提一下,在iOS 8中selectedImageTintColor已经被弃用了。 - chancyWu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接