Error404 Error404 - 1 year ago 78
iOS Question

How can I add right border on an UIView on Swift 3?

I have an

on the top of my screen and I want to add a right border of 1 pixel to it.

Here is the code that I have by the moment:

var border = CALayer()
border.backgroundColor = UIColor(red: 241, green: 10, blue: 9, alpha: 1).cgColor
border.frame = CGRect(x: topView.frame.width + 1, y: 0, width: 1, height: topView.frame.height)

but I am not able to get it work.

What can I do to add a right border to my

Thanks in advance!

Answer Source

This code works for me, You are setting the position of the frame outside of the bounds of the view, change it to - 1 (instead of width + 1) and then it should display as expected. (I changed the colours to make it easier to see in playground)

let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
view.backgroundColor = UIColor.white

let borderLayer = CALayer()
borderLayer.backgroundColor =
borderLayer.frame = CGRect(x: view.frame.width - 1, y: 0, width: 1, height: view.frame.height)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download