视图边框颜色未改变

6

我有一个视图,我使用运行时属性添加边框:

enter image description here

问题在于layer.borderColor,当我设置borderColor时,我的边框消失了,但是当我不设置borderColor时,我有一个黑色的边框,这并不是我想要的。有什么建议吗?


view.layer.borderColor = UIColor.lightGrayColor().CGColor - iDeveloper
首先,“运行时”属性是在代码中设置的。你的屏幕截图来自IB - 这是“设计时”。给我们展示你的代码,也许我们可以帮助你。另外,这些属性是用于什么类型的“视图”?UIView、UIButton等。谢谢。 - user7014451
3个回答

8
您面临此问题是因为layer.borderColor需要CGColor,而从User defined runtime attributes中您只能设置UIColor而不是CGColor。当您没有设置颜色时,它将采用默认的borderColor,即black(黑色)。要设置borderColor,您需要像这样在编程时进行设置。

Swift 3

yourView.layer.borderColor = UIColor.red.cgColor //set your color here

Swift 2.3或更低版本

yourView.layer.borderColor = UIColor.redColor().CGColor //set your color here

2
除了@NiravD建议的内容外,您需要在视图的layer上设置borderColorborderWidth才能看到边框。 Swift 3 & Swift 4:
circle.layer.cornerRadius = 5.0
circle.layer.borderColor = UIColor.red.cgColor
circle.layer.borderWidth = 2.0 

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接