gpt4 book ai didi

ios - 更改 UITabBarItem 未选择的颜色色调 - Swift

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:27 25 4
gpt4 key购买 nike

很简单,我希望能够更改标签栏中未选中项目的颜色。

请参阅下方以默认颜色可读的“查看最多”对象大麦。

这是我尝试实现的代码:

UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIColor.greenColor(), forKey: NSFontAttributeName), forState: UIControlState.Normal)

enter image description here

但是,使用此代码不起作用。有谁知道如何在 swift 中特别实现这种效果?


最佳答案

来自 UITabBarItem 类文档:

By default, the actual unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal.

关键不在于你是否使用UIImageRenderingModeAlwaysOriginal,重要的是什么时候使用它。

要防止未选中的项目显示灰色,您只需要阻止系统为未选中的图像着色。以下是如何做到这一点:

var firstViewController:UIViewController = UIViewController()
// The following statement is what you need
var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
firstViewController.tabBarItem = customTabBarItem

如您所见,我要求 iOS 只为未选择状态应用图像的原始颜色(白色、黄色、红色等),而为选择状态保留图像原样。

此外,您可能需要为标签栏添加一种色调,以便为 SELECTED 状态应用不同的颜色(而不是默认的 iOS 蓝色)。根据上面的屏幕截图,您正在为所选状态应用白色:

self.tabBar.tintColor = UIColor.whiteColor()

关于ios - 更改 UITabBarItem 未选择的颜色色调 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25825543/

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