提问人:RinaredStaring 提问时间:5/5/2023 更新时间:5/5/2023 访问量:100
如果 UITabBarAppearance 处于活动状态,UI 选项卡栏不会更改?
UI Tab bar is not changing if UITabBarAppearance is active?
问:
我正在尝试使用 unselectedItemTintColor 属性更改 UITabBar 中未选择的选项卡项的颜色。但是,当我在代码中设置颜色时,颜色没有改变。下面是相关代码:
init() {
let appearance = UITabBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = UIColor.systemGray6
let image = UIImage.gradientImageWithBounds(
bounds: CGRect(x: 0, y: 0, width: UIScreen.main.scale, height: 20),
colors: [
UIColor.clear.cgColor,
UIColor.black.withAlphaComponent(0.12).cgColor
]
)
appearance.backgroundImage = UIImage()
appearance.shadowImage = image
UITabBar.appearance().standardAppearance = appearance
UITabBar.appearance().unselectedItemTintColor = UIColor(Color("indigo")) // problem here
}
extension UIImage {
static func gradientImageWithBounds(bounds: CGRect, colors: [CGColor]) -> UIImage {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = colors
UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
如您所见,我将 UITabBar 外观设置为自定义 UITabBarAppearance 对象,然后将 unselectedItemTintColor 属性设置为资产目录中定义的自定义颜色 (“indigo”)。
但是,即使所有其他外观属性(例如,背景颜色和阴影图像)正常工作,未选择的选项卡项的颜色也不会更改。
我尝试注释掉除 unselectedItemTintColor 之外的所有其他外观属性,这很有帮助!
我是否遗漏了什么或做错了什么?任何帮助将不胜感激。谢谢!
答: 暂无答案
评论