xcode 视图边框颜色不变
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41424601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
View border Color not changing
提问by
I have a view I make border with this run time attributes:
我有一个视图,我使用此运行时属性制作边框:
The problem is layer.borderColor
when I set borderColor my border is gone but when I don't set border Color I have a black border which I don't want
any ideas?
问题是layer.borderColor
当我设置 borderColor 时,我的边框不见了,但是当我没有设置 border Color 时,我有一个黑色边框,我不想要任何想法?
采纳答案by Nirav D
You are facing this issue because layer.borderColor
want CGColor
and from User defined runtime attributes
you can only set UIColor
not CGColor
, when you don't set the color it will take default borderColor
and i.e black
color. To set borderColor you need to set it programmatically like this.
你正面临这个问题,因为layer.borderColor
想CGColor
和User defined runtime attributes
你只能设置UIColor
不CGColor
,当你不设置将采取默认的颜色borderColor
和IEblack
的颜色。要设置 borderColor,您需要像这样以编程方式设置它。
Swift 3
斯威夫特 3
yourView.layer.borderColor = UIColor.red.cgColor //set your color here
Swift 2.3 or lower
Swift 2.3 或更低版本
yourView.layer.borderColor = UIColor.redColor().CGColor //set your color here
回答by Rbar
In addition to what @NiravD suggested, you need to set both borderColor
and the borderWidth
on the view's layer
to see the border.
除了@NiravD 建议的内容之外,您还需要在视图上同时设置borderColor
和以查看边框。borderWidth
layer
Swift 3 & Swift 4:
斯威夫特 3 和斯威夫特 4:
circle.layer.cornerRadius = 5.0
circle.layer.borderColor = UIColor.red.cgColor
circle.layer.borderWidth = 2.0
回答by Amul4608
in Swift 3.0
在 Swift 3.0 中
@IBOutlet weak var viewMainCell: UIView!
viewMainCell.layer.shadowOpacity = 0.5
viewMainCell.layer.shadowRadius = 5
viewMainCell.layer.shadowColor = UIColor.black.cgColor
viewMainCell.layer.cornerRadius = 5
viewMainCell.layer.masksToBounds = false