-2

i want to change the color of tab bar background and i want to put my own color with color code, how to do that? I have the color code from sketch and dont know how to code that in swift.

Maraz Reis
  • 25
  • 2
  • 7

3 Answers3

2

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#>)
  • When i add this code in the CustomTabBar class, so i get error like "UIColor" has no member "UIColor" – Maraz Reis Nov 02 '16 at 21:50
  • Sorry about that I have miss typed the top one and put UIColor twice should just be UIColor.init(), will edit my answer –  Nov 03 '16 at 01:04
1

Try this code:

navigationController?.toolbar.barTintColor = UIColor.green // You can set to any colour.
Joe
  • 8,868
  • 8
  • 37
  • 59
0

You can specify in AppDelegate.swift how the appearance of your UITabBar will be:

UITabBar.appearance().barTintColor = .white

If you would like a method for custom colors using hex code, you can add an extension to UIColor like this:

extension UIColor {
    convenience init (for hex: Int) {
        let red: Int =        (hex >> 16) & 0xff
        let green: Int =      (hex >> 8)  & 0xff
        let blue: Int =       hex         & 0xff

        assert(
            (red >= 0 && red <= 255) &&
            (green >= 0 && green <= 255) &&
            (blue >= 0 && blue <= 255),
            "bad hex for UIColor"
        )

        self.init (red: CGFloat(red) / 255.0, green: CGFloat (green) / 255.0, blue: CGFloat (blue) / 255.0, alpha: 1.0)
    }

    // Create your own custom color from hex code
    public class var customColor: UIColor {
        return UIColor (for: 0x0067DF)
    }
}

Then you can change your UITabBar background color to your own custom one:

UITabBar.appearance().barTintColor = .customColor
devtbit
  • 94
  • 3