swift - 更改顶部 UIToolbar 边框的颜色

Wal*_*ker 2 uitoolbar swift

我正在尝试更改 UIToolbar 上边框的颜色。

我试过:

layer.borderWidth = 1
layer.borderColor = UIColor(r: 250, g: 250, b: 250).CGColor
Run Code Online (Sandbox Code Playgroud)

那行不通。

建议?谢谢

Wil*_*son 5

我已经使用这段代码完成了:

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)
  }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

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