1

I have followed this Question it is working fine for UITabBar item text but if I use original images then I am unable to change color for selected UITabBar item image

I am changing image tint color and making it original through storyBoard not programmatically i.e "selectedImageTintColor" (this is changing tabbar item image color on selection for template image not for original )

Community
  • 1
  • 1
kahna9
  • 35
  • 11

1 Answers1

0

updated to Swift 3

Use this code inside UITabbarcontroller

    //User this line for selected Tabbar Image tint colour

    UITabBar.appearance().tintColor = UIColor.purple

    // You can set tab colours as per requirement here
    // set red as selected background color
    let numberOfItems = CGFloat(tabBar.items!.count)
    let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
    tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: UIColor.red, size: tabBarItemSize).resizableImage(withCapInsets: UIEdgeInsets.zero)

    // remove default border
    tabBar.frame.size.width = self.view.frame.width + 4
    tabBar.frame.origin.x = -2

And Use this extension for Image color

extension UIImage {
    class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
       let rect: CGRect = CGRect(x: 0,y: 0, width: size.width,height: size.height)
       UIGraphicsBeginImageContextWithOptions(size, false, 0)
       color.setFill()
       UIRectFill(rect)
       let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
       UIGraphicsEndImageContext()
       return image
   }
}
John Codeos
  • 1,060
  • 1
  • 13
  • 17
Anand Nimje
  • 6,163
  • 4
  • 24
  • 43