Swift - 更改标签栏图标的背景颜色,如 Instagram

Swift - Change background color of tab bar icons like Instagram

我想自定义 tabBar 的外观。我想在 Instagram 应用程序上重新创建外观,对于那些不熟悉该应用程序中的标签栏的人,请参阅此图片:Instagram Tab Bar. This is my current tab bar: Current Tab Bar

我已经设置了背景颜色和图像的位置,但我希望所选的标签栏图标的背景颜色比其他图标的背景颜色深。另外,无论是否选中,我都希望中心标签栏图标的颜色不同。

澄清一下,我只需要在选中和未选中时更改选项卡栏图标背景颜色的代码。

提前致谢。

我创建了 UITabBarController 的子类,并在 viewDidLoad 中添加:

let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
let size : CGFloat = CGFloat(itemWidth) * CGFloat(2)
let imageView = UIImageView(frame: CGRectMake(size, 5, itemWidth-10, tabBar.frame.height-10))
let image = UIImage(named: "compose")
imageView.image = image
imageView.contentMode = .ScaleAspectFit
tabBar.insertSubview(imageView, atIndex: 2)

我只是使用了一张带有我想要的背景颜色的图片。 这就是它的样子:

希望这对您有所帮助!