ios - 如何仅在 swift 代码中更改 TabBar 标题字符串的背景颜色

标签 ios swift

enter image description here

我想使用 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/

相关文章:

swift - watchOS LTE 与 iPhone 应用程序通信

ios - 如何在 Swift 中将 UnsafeMutablePointer<AudioBufferList> 转换为 AudioBuffer?

iOS RSA 解密成功但解密数据似乎已损坏

ios - 使用 Tesseract OCR 从图像中提取文本并输入到文本字段中

ios - 从画廊或相机中挑选完图像后,我该如何进行 segue?

ios - Swift - 防止计时器重复

ios - 通过参数将数据从 Swift 中的 iOS 发送到 Rails 应用程序

ios - 内容 View 不显示在 UITableViewCell 上

ios - 具有多个目标的方案 -> Crashlytics -> 无法复制 dSym 文件

iphone - 子类 MKAnnotationView