Théo Fleismaher Théo Fleismaher - 4 months ago 83
iOS Question

Change navigation bar bottom border color Swift

I've seen different answers to this question but non of them is working : I'm trying to change the color of the bottom border of a UINavigationItem. I've tried all the methods of the topics that I've seen but they're not working. Anyone knows how to change it?

Thanks,

Answer

You have to adjust the shadowImage property of the navigation bar.

Try this one. I created a category on UIColor as an helper, but you can refactor the way you prefer.

extension UIColor {
    func as1ptImage() -> UIImage {
        UIGraphicsBeginImageContext(CGSizeMake(1, 1))
        let ctx = UIGraphicsGetCurrentContext()
        self.setFill()
        CGContextFillRect(ctx, CGRect(x: 0, y: 0, width: 1, height: 1))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

And then in your view controller (change the UIColor to what you like):

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIColor.redColor().as1ptImage()