如何在 Swift 3 的 UIView 上添加右边框?
How can I add right border on an UIView on Swift 3?
我的屏幕顶部有一个 UIView
,我想为其添加一个 1 像素的右边框。
这是我目前拥有的代码:
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)
但我无法让它工作。
如何为 UIView
添加右边框?
提前致谢!
顶视图上的 Cliptobounds = false 或将线保持在框架内
这段代码对我有用,您将框架的位置设置在视图边界之外,将其更改为 - 1(而不是宽度 + 1),然后它应该会按预期显示。 (我更改了颜色以便在操场上更容易看到)
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)
我的屏幕顶部有一个 UIView
,我想为其添加一个 1 像素的右边框。
这是我目前拥有的代码:
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)
但我无法让它工作。
如何为 UIView
添加右边框?
提前致谢!
顶视图上的 Cliptobounds = false 或将线保持在框架内
这段代码对我有用,您将框架的位置设置在视图边界之外,将其更改为 - 1(而不是宽度 + 1),然后它应该会按预期显示。 (我更改了颜色以便在操场上更容易看到)
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)