Annabelle Sykes Annabelle Sykes - 3 months ago 16
Swift Question

Change tintColor of custom navigation bar image

I have a navigation bar with an UIImage. I want to change the colour of it, but when I use my code, the default black colour is used. My code is below.

override func viewDidLoad() {
super.viewDidLoad()

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 26, height: 26))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "pinpoint.png")
imageView.image = image
navigationItem.titleView = imageView
imageView.image = UIImage(named: "pinpoint.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
imageView.tintColor = UIColor(red: 22.0/255.0, green: 40.0/255.0, blue: 86.0/255.0, alpha: 1.0)
}


The colour I want to use is on the last line of the snippet.

Answer

You can try this way:

 override func viewDidLoad() {

    super.viewDidLoad()

    let imageView = UIImageView(image: UIImage(named:"img_instagram")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate))
    imageView.contentMode = .ScaleAspectFit
    navigationItem.titleView = imageView
    imageView.tintColor = UIColor(red: 22.0/255.0, green: 40.0/255.0, blue: 86.0/255.0, alpha: 1.0)

}

Hope this help. :)