5
votes

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.

1

1 Answers

8
votes

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?