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
How to Create a Simple Checkbox in iOS
Objective-C 101 (Retain VS Assign) Nsstring
Is There an iPhone Se Simulator for Xcode 11, iOS 13
What Is the "Right" Way to Handle Orientation Changes in iOS 8
Firebase Cloud Messaging - Send Message to All Users
How to Fix Xcode 6.1 Error While Building Ipa
Can Not Save File Inside Tmp Directory
Simple Way to Change the Position of Uiview
Does This App Use the Advertising Identifier (IDFA)? - Admob 6.8.0
How to Draw a "Speech Bubble" on an Iphone
How to Disable Floating Headers in Uitableview with Uitableviewstyleplain
What Are File Owner and First Responder in iOS - Xcode
Cocoapods: Failed to Connect to Github to Update the Cocoapods/Specs Specs Repo
How to Prevent the iPhone Screen from Dimming or Turning Off While My Application Is Running
What Exactly Can Corebluetooth Applications Do Whilst in the Background
Path to Bundle of iOS Framework
In Swift, How to Let Other Apps Continue to Play Audio When My App Is Open
Swiftui - How to Initialize an Observedobject Using an Environmentobject as a Parameter