Changing Tab Bar Color (Swift)

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.

Swift 2.2 How to change background color of tab bar controller

This is a fine way to change the color of a UITabBar. If you want to avoid setting the color in every viewController that is embedded inside of your UITabBarController, you could also create a subclass of UITabBarController and set it there. This way no matter what page comes up first, the color will be set.

To create a subclass of UITabBarController, just go to file > new > file > cocoa touch class...Then setup your file like in this photo

Add File

Now in your storyboard, set the custom class on your tabBarController

Storyboard

Finally, in your file you created MyTabBarController (or whatever you called it):

class MyTabBarController: UITabBarController {

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

self.tabBar.barTintColor = .blueColor()
}
...

Changing Tab Bar Color (Swift)

It doesn't work because all of your RGB components are greater than 1, which is the maximum available value per-channel. You're probably thinking of the color channels as bytes, but that wouldn't scale to varying color bit depths. (For example, it was common to render to RGB565, not RGBA8888 in early versions of iOS. And you can probably expect Apple to make screens with 16-bit accuracy the norm, in the near future.) Floats from 0 to 1 are employed, to divorce the bit depth from the color representation.

https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIColor_Class/index.html#//apple_ref/occ/instm/UIColor/initWithRed:green:blue:alpha:

Changing tab bar item image and text color iOS

From UITabBarItem class docs:

By default, the actual unselected and selected images are
automatically created from the alpha values in the source images. To
prevent system coloring, provide images with
UIImageRenderingModeAlwaysOriginal.

The clue is not whether you use UIImageRenderingModeAlwaysOriginal, the important thing is when to use it.

To prevent the grey color for unselected items, you will just need to prevent the system colouring for the unselected image. Here is how to do this:

var firstViewController:UIViewController = UIViewController()
// The following statement is what you need
var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
firstViewController.tabBarItem = customTabBarItem

As you can see, I asked iOS to apply the original color (white, yellow, red, whatever) of the image ONLY for the UNSELECTED state, and leave the image as it is for the SELECTED state.

Also, you may need to add a tint color for the tab bar in order to apply a different color for the SELECTED state (instead of the default iOS blue color). As per your screenshot above, you are applying white color for the selected state:

self.tabBar.tintColor = UIColor.whiteColor()

EDIT:

Sample Image

How do I change the color of the tab bar for one view controller?

If you have UITabBarControllerDelegate you can use

func tabBarController(_ tabBarController: UITabBarController,
didSelect viewController: UIViewController) {

guard let index = self.viewControllers?.index(where: { $0 == viewController }) else {
return
}
if index == 1 {
self.tabBar.barTintColor = .black
} else {
self.tabBar.barTintColor = .yellow
}
}

Im'm checkig by index because i have navigation controller and this is easier for me, you can try to use

if viewController is MyViewController {
self.tabBar.barTintColor = .black
} else {
self.tabBar.barTintColor = .yellow
}


Related Topics



Leave a reply



Submit