1

I have a UITabBar of which I want to change the background color of the middle item, but I can't figure out how! (I want to keep the rest of the bar the dark grey color it is).

 let barTintColor = UIColor(red: 54/255, green: 54/255, blue: 54/255, alpha: 1.0)
 UITabBar.appearance().barTintColor = barTintColor
Chris Jones
  • 856
  • 1
  • 11
  • 24
  • instead of using the `appearance` proxy set the `barTintColor` of the object, when you use the proxy you modify all the `UITabBar` class instances – tkanzakic Mar 25 '15 at 07:49

1 Answers1

3

You can do this by inserting a new subview to your TabBar.
Please check out this answer:

// 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)

Hope it helps.

Edit:
If you want to change the background image rather than background color, all you have to do is change the line :

bgView.backgroundColor = bgColor

to imageView with image as background, then add it as a subview. It may look like this:

backgroundView = UIImageView(image: UIImage(named: "tabBarImage"))
bgView.addSubview(backgroundView)
Community
  • 1
  • 1
Fengson
  • 4,751
  • 8
  • 37
  • 62
  • Suppose I want to change the color of the text (and image) for a particular item (not the background color). Any ideas? Thanks fengson! – Chris Jones Mar 25 '15 at 08:20