How to change the color of navbar ios

2

I'm developing an application, I'd like the navigation bar to have the same body color as the project, I'm trying with the following code

        UINavigationBar.appearance().barTintColor = UIColor.redColor()

This code works, changes to red, but I wanted another color and tried the following code

navigationBarAppearace.tintColor = UIColor.init(red: 23, green: 27, blue: 113, alpha: 1) 

I also tried this one

let corBarra = UIColor(red: 24, green: 27, blue: 113, alpha: 1)
UINavigationBar.appearance().barTintColor = corBarra

Does anyone have a clue how to change to a color that is no longer pre-defined?

    
asked by anonymous 19.08.2016 / 01:09

1 answer

5

The problem is that you have to pass a value between 0.0 and 1.0. If you pass any value above it it interprets as 1.

let corBarra = UIColor(red: 24/255, green: 27/255, blue: 113/255, alpha: 1)
UINavigationBar.appearance().barTintColor = corBarra

If you want, you can also create your own initializer:

extension UIColor {
    convenience init(r: Int, g: Int , b: Int , alpha: CGFloat) {
        self.init(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: alpha)
    }
}
let corBarra = UIColor(r: 24 , g: 27, b: 113, alpha: 1)   // r 0.094 g 0.106 b 0.443 a 1.0
    
19.08.2016 / 05:07