【问题标题】:Change background color of single bar bar item - swift?更改单条栏项目的背景颜色 - 快速?
【发布时间】:2015-03-25 07:43:01
【问题描述】:

我有一个 UITabBar 我想更改中间项目的背景颜色,但我不知道如何! (我想让酒吧的其余部分保持深灰色)。

 let barTintColor = UIColor(red: 54/255, green: 54/255, blue: 54/255, alpha: 1.0)
 UITabBar.appearance().barTintColor = barTintColor

【问题讨论】:

  • 而不是使用appearance代理设置对象的barTintColor,当你使用代理你修改所有UITabBar类实例

标签: swift


【解决方案1】:

您可以通过在 TabBar 中插入一个新的子视图来做到这一点。
请查看this 答案:

// Add background color to middle tabBarItem
let itemIndex = 2
let bgColor = UIColor(red: 0.08, green: 0.726, blue: 0.702, alpha: 1.0)

let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
let bgView = UIView(frame: CGRectMake(itemWidth * itemIndex, 0, itemWidth, tabBar.frame.height))
bgView.backgroundColor = bgColor
tabBar.insertSubview(bgView, atIndex: 0)

希望对你有帮助。

编辑:
如果您想更改background image 而不是background color,您只需更改行:

bgView.backgroundColor = bgColor

以图像为背景的imageView,然后将其添加为子视图。它可能看起来像这样:

backgroundView = UIImageView(image: UIImage(named: "tabBarImage"))
bgView.addSubview(backgroundView)

【讨论】:

  • 假设我想更改特定项目的文本(和图像)颜色(不是背景颜色)。有任何想法吗?谢谢冯森!
猜你喜欢
  • 2017-10-04
  • 2021-10-25
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多