Search code examples
iosswiftuinavigationcontrolleruinavigationbaruicolor

How to set custom (RGB) color to Navigation bar?


I cannot to set my RGB color to UINavigationBarand ToolBar. I tried it

  let myColor = UIColor(red: 47, green: 206, blue: 255, alpha: 1.0)
  self.navigationController?.navigationBar.barTintColor = myColor
  self.navigationController?.toolbar.tintColor = myColor

Also I tried HSB color

let secondColor = UIColor(hue: 194, saturation: 82, brightness: 100, alpha: 1.0)
self.navigationController?.navigationBar.barTintColor = secondColor
self.navigationController?.toolbar.tintColor = secondColor

But when I wrote the following method it works.

self.navigationController?.navigationBar.barTintColor = UIColor.greenColor()
self.navigationController?.toolbar.tintColor = UIColor.greenColor()

How can I set my RGB color to bars?


Solution

  • let myColor = UIColor(red: 47, green: 206, blue: 255, alpha: 1.0)
    

    is incorrect. UIColor requires color components in a range 0.0 ... 1.0. So you probably need

    let myColor = UIColor(red: 47.0/255.0, green: 206.0/255.0, blue: 255.0/255.0, alpha: 1.0)
    

    From the UIColor docs

    enter image description here