IosMohammed
IosMohammed

Reputation: 1

How to change the background color of TabBar title string only in swift code

enter image description here

I'd like to put a background color for just the title of TabBar using swift I tried to use NSAttributedString.Key.backgroundColor but it not appears color in the background.

Can anybody help me to change the background color for just the title of TabBar?

I need to add yellow color to the background to title in the normal state not selected.

Upvotes: 0

Views: 249

Answers (2)

Ewurafua Plange
Ewurafua Plange

Reputation: 656

you can add the following code snippet to your viewDidLoad:

UITabBar.appearance().barTintColor = UIColor.black // your color

Upvotes: 0

Tudor Andreescu
Tudor Andreescu

Reputation: 138

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.gray], for: .selected)

This works for a single item. If you'd like to change the color for all items in the tab bar, try parsing through them like such

for item in self.tabBar.items! {

      item.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
      item.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.gray], for: .selected)

    } 

Upvotes: 1

Related Questions