title: UITabBarItem Tab Bar Item Individual Selected Background Color
body: <p>In my project, I am using an extension of UIImage to create change the selected background color of the tab bar items:</p>
  
  <pre><code>extension UIImage {
  func imageWithColor(tintColor: UIColor) -&gt; UIImage {
      UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
  
      let context = UIGraphicsGetCurrentContext() as CGContextRef!
      CGContextTranslateCTM(context, 0, self.size.height)
      CGContextScaleCTM(context, 1.0, -1.0);
      CGContextSetBlendMode(context, CGBlendMode.Normal)
  
      let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
      CGContextClipToMask(context, rect, self.CGImage)
      tintColor.setFill()
      CGContextFillRect(context, rect)
  
      let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
      UIGraphicsEndImageContext()
  
      return newImage
  }
  
  func makeImageWithColorAndSize(color: UIColor, size: CGSize) -&gt; UIImage {
      UIGraphicsBeginImageContextWithOptions(size, false, 0)
      color.setFill()
      UIRectFill(CGRectMake(0, 0, 100, 100))
      let image = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      return image
  }   
  }
  </code></pre>
  
  <p>And then using that in my MainTabBarController</p>
  
  <pre><code> class MainTabBarController: UITabBarController {
   override func viewDidLoad() {
      super.viewDidLoad()
  
      let itemIndex = 0
      let bgColor = UIColor(red: 194/255.0, green: 39/255.0, blue: 65/255.0, alpha: 1.0)
  
      let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
      let frame = CGRectMake(CGFloat(itemWidth) * CGFloat(itemIndex), 0, itemWidth, tabBar.frame.height)
      let bgView = UIView(frame: frame)
  
      bgView.backgroundColor = bgColor
      tabBar.insertSubview(bgView, atIndex: 0)
  
      for item in self.tabBar.items as [UITabBarItem]! {
          if let image = item.image {
              item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
          }
  
  
          // Sets the default color of the icon of the selected UITabBarItem and Title
          UITabBar.appearance().tintColor = UIColor.whiteColor()
  
          // Sets the default color of the background of the UITabBar
          UITabBar.appearance().barTintColor = UIColor(red: 39/255.0, green: 39/255.0, blue: 39/255.0, alpha: 1.0)
  
          // Sets the background color of the selected UITabBarItem (using and plain colored UIImage with the width = 1/5 of the tabBar (if you have 5 items) and the height of the tabBar)
  
  
          print(UITabBar.appearance().selectionIndicatorImage)
  
  
  
          UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor(red: 21/255.0, green: 21/255.0, blue: 21/255.0, alpha: 1.0), size: CGSizeMake(tabBar.frame.width/4, tabBar.frame.height))
  
      }
  
      // Do any additional setup after loading the view.
  }
  </code></pre>
  
  <p>This works, but how can I change the selected background color for individual tab bar items?</p>