我想使用 swift 为 TabBar 的 title
设置背景色 我尝试使用 NSAttributedString.Key.backgroundColor
但它在背景。
谁能帮我改变 TabBar 的 title
的背景颜色?
我需要在未选中的正常状态下为标题添加黄色背景。
最佳答案
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.gray], for: .selected)
这适用于单个项目。如果您想更改标签栏中所有项目的颜色,请尝试像这样解析它们
for item in self.tabBar.items! {
item.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
item.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.gray], for: .selected)
}
关于ios - 如何仅在 swift 代码中更改 TabBar 标题字符串的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57035797/