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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:41:50  来源:igfitidea点击:

View border Color not changing

swiftxcodestoryboardborderlayer

提问by

I have a view I make border with this run time attributes:

我有一个视图,我使用此运行时属性制作边框:

enter image description here

在此处输入图片说明

The problem is layer.borderColorwhen 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.borderColorwant CGColorand from User defined runtime attributesyou can only set UIColornot CGColor, when you don't set the color it will take default borderColorand i.e blackcolor. To set borderColor you need to set it programmatically like this.

你正面临这个问题,因为layer.borderColorCGColorUser defined runtime attributes你只能设置UIColorCGColor,当你不设置将采取默认的颜色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 borderColorand the borderWidthon the view's layerto see the border.

除了@NiravD 建议的内容之外,您还需要在视图上同时设置borderColor和以查看边框。borderWidthlayer

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