How to change the color of the Ios navbar

Asked

Viewed 464 times

2

I am developing an application, I would like the navigation bar to have the same color as the body of the project, I am trying with the following code

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

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

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

and also tried that other

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

Does anyone have any idea how to change to a color that is no longer preset?

1 answer

5


The problem is that Voce has to pass a value between 0.0 and 1.0. If Voce passes any value above that 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 Voce 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
  • 1

    Thank you Leo, solved my problem. Thank you

  • Nothing Kleiton. If you need help with the date in iso8601 format let me know. Abs

  • Leo, another expensive question You can tell me how I can change the text of the "back" button of the navbar, the default appears "<back" wanted to put to "<back" or an image that become more intuitive

  • So in my head I don’t remember but I think it’s right to always use localized string. I’ll look for some reference and tell you. Open another question.

  • Okay Leo, thanks for the layout, I’ll create a question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.