ios 如何以编程方式设置 UIView 的自定义边框颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29700919/
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
How to set the custom border color of UIView programmatically?
提问by Shashi Verma
I am trying to set the custom border color of UIView programmatically in Swift.
我正在尝试在 Swift 中以编程方式设置 UIView 的自定义边框颜色。
回答by Shashi Verma
If you Use Swift 2.0+
如果您使用 Swift 2.0+
self.yourView.layer.borderWidth = 1
self.yourView.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor
回答by Paras Joshi
In Swift 4you can set the border color and width to UIControls using below code.
在Swift 4 中,您可以使用以下代码将边框颜色和宽度设置为 UIControls。
let yourColor : UIColor = UIColor( red: 0.7, green: 0.3, blue:0.1, alpha: 1.0 )
yourControl.layer.masksToBounds = true
yourControl.layer.borderColor = yourColor.CGColor
yourControl.layer.borderWidth = 1.0
< Swift 4, You can set UIView's border width and border color using the below code.
< Swift 4,您可以使用以下代码设置 UIView 的边框宽度和边框颜色。
yourView.layer.borderWidth = 1
yourView.layer.borderColor = UIColor.red.cgColor
回答by Supratik Majumdar
Use @IBDesignableand @IBInspectableto do the same.
使用@IBDesignable和@IBInspectable来做同样的事情。
They are re-useable, easily modifiable from the Interface Builder and the changes are reflected immediately in the Storyboard
它们是可重复使用的,可以从 Interface Builder 轻松修改,并且更改会立即反映在 Storyboard 中
Conform the objects in the storyboard to the particular class
使故事板中的对象符合特定的类
Code Snippet:
代码片段:
@IBDesignable
class CustomView: UIView{
@IBInspectable var borderWidth: CGFloat = 0.0{
didSet{
self.layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor = UIColor.clear {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
}
}
Allows easy modification from Interface Builder:
允许从 Interface Builder 轻松修改:
回答by Shardul
You can write an extension to use it with all the UIViews eg. UIButton, UILabel, UIImageView etc. You can customise my following method as per your requirement, but I think it will work well for you.
您可以编写一个扩展以将其与所有 UIViews 一起使用,例如。UIButton、UILabel、UIImageView 等。您可以根据您的要求自定义我的以下方法,但我认为它对您很有效。
extension UIView{
func setBorder(radius:CGFloat, color:UIColor = UIColor.clearColor()) -> UIView{
var roundView:UIView = self
roundView.layer.cornerRadius = CGFloat(radius)
roundView.layer.borderWidth = 1
roundView.layer.borderColor = color.CGColor
roundView.clipsToBounds = true
return roundView
}
}
Usage:
用法:
btnLogin.setBorder(7, color: UIColor.lightGrayColor())
imgViewUserPick.setBorder(10)
回答by Deepak Tagadiya
swift 3
迅捷 3
func borderColor(){
self.viewMenuItems.layer.cornerRadius = 13
self.viewMenuItems.layer.borderWidth = 1
self.viewMenuItems.layer.borderColor = UIColor.white.cgColor
}
回答by ikbal
Swift 5.2, UIView+Extension
Swift 5.2, UIView+扩展
extension UIView {
public func addViewBorder(borderColor:CGColor,borderWith:CGFloat,borderCornerRadius:CGFloat){
self.layer.borderWidth = borderWith
self.layer.borderColor = borderColor
self.layer.cornerRadius = borderCornerRadius
}
}
You used this extension;
您使用了此扩展程序;
yourView.addViewBorder(borderColor: #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1), borderWith: 1.0, borderCornerRadius: 20)
回答by Mr.Javed Multani
We can create method for it. Simply use it.
我们可以为它创建方法。只需使用它。
public func createBorderForView(color: UIColor, radius: CGFloat, width: CGFloat = 0.7) {
self.layer.borderWidth = width
self.layer.cornerRadius = radius
self.layer.shouldRasterize = false
self.layer.rasterizationScale = 2
self.clipsToBounds = true
self.layer.masksToBounds = true
let cgColor: CGColor = color.cgColor
self.layer.borderColor = cgColor
}
回答by Chathura Jayanaka
swift 3.0
迅捷 3.0
self.uiTextView.layer.borderWidth = 0.5
self.txtItemShortDes.layer.borderColor = UIColor(red:205.0/255.0, green:205.0/255.0, blue:205.0/255.0, alpha: 1.0).cgColor
回答by Ethan Joe
Write the code in your viewDidLoad()
把代码写在你的 viewDidLoad()
self.view.layer.borderColor = anyColor().CGColor
And you can set Color
with RGB
您可以设置Color
与RGB
func anyColor() -> UIColor {
return UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)
}
Learn something about CALayer
in UIKit
了解一些关于CALayer
在UIKit
回答by Вадим Башуров
Swift 3.0
斯威夫特 3.0
groundTrump.layer.borderColor = UIColor.red.cgColor