If I set the same UIColor with alpha to a UIView and its border, the colors do not match. The strange thing is, that the border gets an alpha value (it’s not completely black), but it’s not matching the view’s background.
Example
Code:
let color = UIColor(white: 0, alpha: 0.3)
let view = UIView(frame: .zero)
view.backgroundColor = color
view.layer.borderWidth = 5
view.layer.borderColor = color.cgColor
2
Answers
Your issue is that when you set the same semi-transparent UIColor to both the background and border of a UIView, the resulting colors don’t visually match. The border appears differently than the background, even though you’re using the same color value for both. Although adding a border with same color of the background is useless.
If you want independent "inner" and "outer" colors, there are various ways to go about it.
Probably the most straight-forward approach is to create a custom
UIView
subclass and add an "inner"UIView
as a subview.The base view will have
backgroundColor = .clear
and thelayer.borderWidth
andlayer.borderColor
.The "inner" view will be inset by
borderWidth
.