我试图设置自定义边框颜色的 UIView 在 Swift 编程。
在 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.
yourView.layer.borderWidth = 1 yourView.layer.borderColor = UIColor.red.cgColor
在 viewDidLoad()中编写代码
viewDidLoad()
self.view.layer.borderColor = anyColor().CGColor
你可以用 RGB设置 Color
RGB
Color
func anyColor() -> UIColor { return UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0) }
在 UIKit中学习一些关于 CALayer的知识
UIKit
CALayer
如果你使用 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
您可以编写一个扩展来使用它与所有的 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)
Swift 3.0
groundTrump.layer.borderColor = UIColor.red.cgColor
Use @ IBDesignable and @ IBChectable to do the same.
这些 Interface Builder 可以重复使用,并且很容易从故事板中进行修改,而且修改会立即反映在故事板中
使故事板中的对象符合特定的类
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() } }
允许简单的 Interface Builder 修改:
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
雨燕3
func borderColor(){ self.viewMenuItems.layer.cornerRadius = 13 self.viewMenuItems.layer.borderWidth = 1 self.viewMenuItems.layer.borderColor = UIColor.white.cgColor }
我们可以为它创建方法,只要使用它。
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 }
Swift 5.2 ,< strong > UIView + 扩展
extension UIView { public func addViewBorder(borderColor:CGColor,borderWith:CGFloat,borderCornerRadius:CGFloat){ self.layer.borderWidth = borderWith self.layer.borderColor = borderColor self.layer.cornerRadius = borderCornerRadius } }
您使用了这个分机;
yourView.addViewBorder(borderColor: #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1), borderWith: 1.0, borderCornerRadius: 20)
Swift 5 *
我,总是使用视图扩展,使视图角落圆,设置边框颜色和宽度,它一直是我最方便的方式。只需复制粘贴此代码并在属性检查器中控制这些属性即可。
extension UIView { @IBInspectable var cornerRadius: CGFloat { get { return layer.cornerRadius } set { layer.cornerRadius = newValue } } @IBInspectable var borderWidth: CGFloat { get { return layer.borderWidth } set { layer.borderWidth = newValue } } @IBInspectable var borderColor: UIColor? { get { if let color = layer.borderColor { return UIColor(cgColor: color) } return nil } set { if let color = newValue { layer.borderColor = color.cgColor } else { layer.borderColor = nil } } } }
如果你正在处理的层,你可以给这样的边界... 。 如果没有的话,你可能需要像 < strong > self. anytextField.wanslayer = true 这样的对象
var BackgroundColor : CGColor = UIColor.blue.cgColor subLayer.shadowColor = self.BackgroundColor subLayer.borderColor = self.BackgroundColor subLayer.borderWidth = CGFloat(4)
或者您可以为 macOS 使用 CA 层的扩展
func addBorder(edge: NSRectEdge, color: NSColor, thickness: CGFloat) { let border = CALayer() switch edge { case NSRectEdge.maxY: border.frame = CGRect(x: 0, y: 0, width: frame.width, height: thickness) case NSRectEdge.minY: border.frame = CGRect(x:0, y: frame.height - thickness, width: frame.width, height:thickness) case NSRectEdge.minX: border.frame = CGRect(x:0, y:0, width: thickness, height: frame.height) case NSRectEdge.maxX: border.frame = CGRect(x: frame.width - thickness, y: 0, width: thickness, height: frame.height) default: do {} } border.backgroundColor = color.cgColor addSublayer(border) }
它的用途如下
subLayer.addBorder(edge: <#T##NSRectEdge#>, color: <#T##NSColor#>, thickness: <#T##CGFloat#>)