Joe Joe - 1 month ago 38
Swift Question

Adding top only border to UIToolBar in Swift

I am trying to add a top only border to a UIToolBar with define color.Currently, I am using the following code to give a transparent look.ToolBar looks ok.But, I am getting a black top border line in the toolBar.I want to change that black border to white.

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


Thanks in Advance....

Answer

Try this:

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)