简体   繁体   中英

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

I have an UIView 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)
topView.layer.addSublayer(border)

but I am not able to get it work.

What can I do to add a right border to my UIView ?

Thanks in advance!

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 = UIColor.red.cgColor
borderLayer.frame = CGRect(x: view.frame.width - 1, y: 0, width: 1, height: view.frame.height)
view.layer.addSublayer(borderLayer)

topview上的Cliptobounds = false或保持框架内的线条

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM