4

I have tab bar and I want to change icons color from default gray to white, I added this line in AppDelegate

UITabBar.appearance().barTintColor = UIColor(red:0.51, green:0.39, blue:0.37, alpha:1.0)

this is change selected item, How I acn do that with non selected?

joda
  • 711
  • 2
  • 8
  • 16
  • If you are setting image to tab bar check this once http://stackoverflow.com/a/38560183/6433023 – Nirav D Aug 31 '16 at 10:06
  • Possible duplicate of these similar questions: http://stackoverflow.com/questions/30754026 and http://stackoverflow.com/questions/29876722 – Derek Soike Jan 09 '17 at 22:05

4 Answers4

2

For Swift use below:

self.tabBar.unselectedItemTintColor = UIColor.white
Dheeraj D
  • 4,386
  • 4
  • 20
  • 34
1

Change according to below code for Tab bar controller for change tab item selected and Deselected color.

class TabbarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UITabBar.appearance().tintColor = UIColor.purpleColor()

        // 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(UIColor.lightTextColor().colorWithAlphaComponent(0.5), size: tabBarItemSize).resizableImageWithCapInsets(UIEdgeInsetsZero)

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

    }

    override func viewWillAppear(animated: Bool) {
        // For Images
        let firstViewController:UIViewController = NotificationVC()
        // The following statement is what you need
        let customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "notification@2x")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "notification_sel@2x"))
        firstViewController.tabBarItem = customTabBarItem

        for item in self.tabBar.items! {
            let unselectedItem = [NSForegroundColorAttributeName: UIColor.whiteColor()]
            let selectedItem = [NSForegroundColorAttributeName: UIColor.purpleColor()]

            item.setTitleTextAttributes(unselectedItem, forState: .Normal)
            item.setTitleTextAttributes(selectedItem, forState: .Selected)
        }
    } 
}

extension UIImage {
    class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
        let rect: CGRect = CGRectMake(0, 0, size.width, size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        UIRectFill(rect)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

}
Anand Nimje
  • 6,163
  • 4
  • 24
  • 43
1

Assuming you have made your icons in the colour that you wanted them to be (in photoshop or sketch or whatever), you can do this via the attributes inspector.

Go to your storyboard, find your view controller (whcih should have been embedded in the tab bar controller already) and select the tab bar at the bottom of the view controller.

In the attributes inspector set the "Image" under Bar Item to your unselected tab bar item image (which should be in your assets already) and set the "Selected Image" under Tab Bar Item to your selected version.

Next, go to your asset catalogue, select your image and in the attributes inspector, under Image Set, set the Render As to Original Image. Do this for all your icons.

This should now work

pho_pho
  • 672
  • 11
  • 30
1

Support for iOS 14 and iOS 15:

    if #available(iOS 15.0, *) {
        let appearance = UITabBarAppearance()

        appearance.backgroundColor = bgColor
        appearance.stackedLayoutAppearance.normal.iconColor = unselectedColor
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unselectedColor]

        tabBarAppearance.standardAppearance = appearance
        tabBarAppearance.scrollEdgeAppearance = appearance
    } else {
        tabBarAppearance.backgroundColor = bgColor
        tabBarAppearance.barTintColor = bgColor
        tabBarAppearance.unselectedItemTintColor = unselectedColor
    }
SoftDesigner
  • 5,640
  • 3
  • 58
  • 47