在 Swift 中向 UIToolBar 添加仅顶部边框

Adding top only border to UIToolBar in Swift

我正在尝试使用定义 color.Currently 向 UIToolBar 添加仅顶部边框,我正在使用以下代码提供透明 look.ToolBar 外观 ok.But,我正在toolBar.I 中的黑色顶部边框线想要将黑色边框更改为白色。

    My Code:
    navigationController?.toolbar.setBackgroundImage(UIImage(), forToolbarPosition: UIBarPosition.any, barMetrics: UIBarMetrics.default)
    navigationController?.toolbar.barStyle = UIBarStyle.default
    navigationController?.toolbar.tintColor = UIColor.white
    navigationController?.toolbar.backgroundColor = UIColor(white: 0.6, alpha: 0.2)

    // To remove the black border line. If, I want to
    self.navigationController?.toolbar.clipsToBounds = true


   // I don't want to put a border around the UIToolbar like below code
    self.navigationController?.toolbar.layer.borderColor = UIColor.white.cgColor
    self.navigationController?.toolbar.layer.borderWidth = 0.2

提前致谢....

试试这个:

let path = UIBezierPath()
    path.move(to: CGPoint(x: mytabbr.bounds.minX, y: mytabbr.bounds.minY ))
    path.addLine(to: CGPoint(x: mytabbr.bounds.maxX, y: mytabbr.bounds.minY ))

    let shape = CAShapeLayer()

    shape.path = path.cgPath
    shape.strokeColor = UIColor.black.cgColor
    shape.fillColor = UIColor.clear.cgColor
    shape.lineWidth = 2
    shape.lineCap = kCALineCapRound

    mytabbr.layer.addSublayer(shape)

我刚刚搞清楚 answer.Simply ,我在工具栏上方放置了一个细线 UIView。

  let lineView = UIView(frame: CGRect(x:0, y:0, width:view.frame.width, height:0.2))
  lineView.backgroundColor=UIColor.white.withAlphaComponent(0.6)
  self.navigationController?.toolbar.addSubview(lineView)

我使用下面的代码从工具栏中删除了现有的黑色边框

 self.navigationController?.toolbar.clipsToBounds = true

以上代码有效且看起来 great.Any,改进答案将不胜感激....