iOS 8 Tab Bar Item Background Colour

Edit background color of specific Tab bar item

You can use the following code :

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

For reference you can look into these Stackoverflow posts :

  • Change background Color of One UITabBarItem
  • Set background Color of Active Tab bar item

Changing the background color of Tab Bar

To change background colour of UITabBar

TabBarController* Tcontroller =(TabBarController*)self.window.rootViewController;
Tcontroller.tabBar.barTintColor=[UIColor yourcolour];

Swift 3

Based on the code above, you can get it by doing this

let Tcontroller = self.window.rootViewController as? UITabBarController
Tcontroller?.tabBar.barTintColor = UIColor.black // your color

or in more general

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

How to change the background color of tab bar programatically?

You should use self.tabBar.barTintColor or have a look at UIBarStyle and self.tabBar.barStyle and see if that works.

Custom tab bar background color. How to change color of tab bar background?

In the viewController class for your tab bar put one of the following sections of code into your view did load depending on what values you have.

self.tabBar.barTintColor = UIColor.init(red: <#T##CGFloat#>, green: <#T##CGFloat#>, blue: <#T##CGFloat#>, alpha: <#T##CGFloat#>)

or

self.tabBar.barTintColor = UIColor.init(hue: <#T##CGFloat#>, saturation: <#T##CGFloat#>, brightness: <#T##CGFloat#>, alpha: <#T##CGFloat#>)


Related Topics



Leave a reply



Submit