简体   繁体   English

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

[英]Swift - Change background color of tab bar icons like Instagram

I'd like to customize the appearance of a tabBar. 我想自定义tabBar的外观。 I'd like to recreate the look on the Instagram app, for those unfamiliar with the tab bar in that app, see this image: Instagram Tab Bar . 我想在Instagram应用上重新创建外观,对于那些不熟悉该应用中标签栏的人,请参见此图片: Instagram Tab Bar This is my current tab bar: Current Tab Bar . 这是我当前的标签栏: 当前标签栏

I've got the background color set and the position of the images but I'd like the selected tab bar icon to have a darker background color to the rest of them. 我已经设置了背景颜色和图像的位置,但是我希望所选的标签栏图标具有比其他图像更暗的背景颜色。 Also, I'd like the center tab bar icon to be a different color regardless of whether or not it's selected. 另外,无论是否选择,我都希望中心标签栏图标为不同的颜色。

To clarify, I just need the code to change the background color of a tab bar icon when selected and not selected. 为了澄清,我只需要代码即可在选中和未选中时更改选项卡图标的背景颜色。

Thanks in advance. 提前致谢。

I had create a subclass of UITabBarController and in the viewDidLoad add this: 我已经创建了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)

I just used an image with the background color that i wanted. 我只使用了想要的背景色的图像。 That is how it's looks like: 这就是它的样子: 的TabBar

Hope this could help! 希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM