Search code examples
swiftuitoolbar

swift - change color of top UIToolbar border


I'm trying to change the color of the top border on a UIToolbar.

I tried:

layer.borderWidth = 1
layer.borderColor = UIColor(r: 250, g: 250, b: 250).CGColor

That didn't work.

Suggestions? Thank you


Solution

  • I've done it using this code:

    class ViewController: UIViewController {
      
      @IBOutlet weak var toolBar: UIToolbar!
      
      override func viewDidLoad() {
        super.viewDidLoad()
        
        // the layer with the width of the device
        // and height of 0.5 px
        let topBorder = CALayer()
        topBorder.frame = CGRectMake(0, 0, view.frame.size.width, 0.5)
        topBorder.backgroundColor = UIColor.redColor().CGColor
        
        // adding the layer to the top
        // of the toolBar
        toolBar.layer.addSublayer(topBorder)
      }
    }
    

    Result:

    enter image description here

    Source: https://blog.adamcooke.io/set-the-top-border-colour-of-a-uinavbar-d9035c6b4fdb#.f37molpsj