Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change tint color of tab bar in swift?

I'm using tab bar and I have 2 problems with color.

1st Problem, the tint color is grey, I used some code to change it to white but it turn to white only when tab is pressed.

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let barColor = UIColor(red: 49/255, green: 75/255, blue: 108/255, alpha: 1.0)
    let pressedTintColor = UIColor.whiteColor()

    UITabBar.appearance().barTintColor = barColor
    UITabBar.appearance().tintColor = pressedTintColor

        return true
}

2nd Problem, the background color of pressed tab is supposed to change but it's not changing.

This is how tab bar looks. enter image description here

And this is how it's supposed to look.enter image description here

(1st pic is in Xcode Simulator just as test, 2nd pic is design of it, so it's not important to much about images and text of tabs)

So it's supposed all tabs to be all the time white, and when a tab is pressed to change just background color of tab.

like image 906
Emm Avatar asked Nov 07 '15 14:11

Emm


People also ask

How do I change the tint color of a bar item?

Use the Image Tint (selectedImageTintColor) field to specify the bar item’s tint color when that tab is selected. By default, that color is blue.

How to change the color of tab bar in Swift 3?

Swift 3 First of all, make sure you have added the BOOLEAN key "View controller-based status bar appearance" to Info.plist, and set the value to "NO". Appdelegate.swift Insert code somewhere after "launchOptions:[UIApplicationLaunchOptionsKey: Any]?) -> Bool {" Change the color of the tab bar itself with RGB color value:

How to remove the tint on the image in the Tabbar?

Image setup- from the storyboard set Bar Item Image and Selected Image. To remove the tint overlay on the images go to Assets catalog, select the image and change its rendering mode like this: This will prevent the Tab bar component from setting its default image tint.

How do I remove the tint overlay on the images?

To remove the tint overlay on the images go to Assets catalog, select the image and change its rendering mode like this: This will prevent the Tab bar component from setting its default image tint.


1 Answers

To solve the problems in your AppDelegate do this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    UITabBar.appearance().tintColor = UIColor.whiteColor()
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)

    return true
}

And in your ViewController do something like this:

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

extension UIImage {
    func imageWithColor(tintColor: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

        let context = UIGraphicsGetCurrentContext()! as CGContextRef
        CGContextTranslateCTM(context, 0, self.size.height)
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSetBlendMode(context, CGBlendMode.Normal)

        let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
        CGContextClipToMask(context, rect, self.CGImage)
        tintColor.setFill()
        CGContextFillRect(context, rect)

        let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
        UIGraphicsEndImageContext()

        return newImage
    }
}

class FirstViewController: UIViewController {

    var tabBar: UITabBar?

    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar = self.tabBarController!.tabBar
        tabBar!.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar!.frame.width/CGFloat(tabBar!.items!.count), tabBar!.frame.height))

        // To change tintColor for unselected tabs
        for item in tabBar!.items! as [UITabBarItem] {
            if let image = item.image {
                item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

*The first extension to UIImage is taken from another question by the same author: How to change default grey color of tab bar items?

like image 71
Alexey Pichukov Avatar answered Nov 15 '22 09:11

Alexey Pichukov