Théo Fleismaher Théo Fleismaher - 1 year ago 264
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?


Answer Source

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()
        CGContextFillRect(ctx, CGRect(x: 0, y: 0, width: 1, height: 1))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download