layer.border不起作用?

时间:2018-01-16 08:11:56

标签: swift uiview

let view1 = UIView(frame: CGRect(x: 0, y: 100, width: 200, height: 180))
let view1sub = UIView(frame: CGRect(x: 50, y: 50, width: 90, height: 90))

view1.addSubview(view1sub)
view1sub.backgroundColor = UIColor.yellow
view1.backgroundColor = UIColor.blue

 view1sub.backgroundColor = UIColor.red

enter image description here

view1.layer.borderWidth =  3.0
view1.layer.borderColor = UIColor.purple.cgColor
view1sub.layer.borderWidth = 2.0
view1sub.layer.borderColor = UIColor.red.cgColor

enter image description here

我使用上面的代码为view1添加边框。

我想使用相同的代码为view1sub添加边框。

令我惊讶的是,view1sub的背景颜色也发生了变化。

1 个答案:

答案 0 :(得分:2)

layer.border适用于 view1sub 。您只是无法看到,因为您的 view1sub 背景颜色为红色。将背景更改为您可以看到的其他颜色。

相关问题