简体   繁体   中英

Swift - Change background color of tab bar icons like Instagram

I'd like to customize the appearance of a 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 . 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:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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