如何在UIView的顶端添加一个边框

我的问题是在标题上。

我不知道如何添加边框在特定的一面,顶部或底部,任何一方… layer.border绘制整个视图的边框…

我认为子类化UIView和重写drawRect在这里矫枉过正。 为什么不在UIView上添加一个类别并使用CALayer

 - (void)prefix_addUpperBorder { CALayer *upperBorder = [CALayer layer]; upperBorder.backgroundColor = [[UIColor greenColor] CGColor]; upperBorder.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 1.0f); [self.layer addSublayer:upperBorder]; } 

有了这个代码,你也不会被绑定到你的子类,你可以将它应用于任何东西,从UIViewinheritance的所有东西 – 可以在你的项目中重用,以及任何其他东西。 您甚至可以添加一个typedef ,并使该方法switch以确定要渲染边框的边。 将其他parameter passing给您的方法来定义其他颜色和宽度。 很多select。

值得一提的是使用CALayer将需要#import <QuartzCore/QuartzCore.h> 。 我通常在预编译头文件这样的全局位置执行此操作。


编辑05/05/15

我开始需要我在原始答案中描述的function – “你甚至可以添加一个typedef ,并使方法switch来确定边界的边界”

我想我也可以把它添加到这个答案:

 - (CALayer *)prefix_addUpperBorder:(UIRectEdge)edge color:(UIColor *)color thickness:(CGFloat)thickness { CALayer *border = [CALayer layer]; switch (edge) { case UIRectEdgeTop: border.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), thickness); break; case UIRectEdgeBottom: border.frame = CGRectMake(0, CGRectGetHeight(self.frame) - thickness, CGRectGetWidth(self.frame), thickness); break; case UIRectEdgeLeft: border.frame = CGRectMake(0, 0, thickness, CGRectGetHeight(self.frame)); break; case UIRectEdgeRight: border.frame = CGRectMake(CGRectGetWidth(self.frame) - thickness, 0, thickness, CGRectGetHeight(self.frame)); break; default: break; } border.backgroundColor = color.CGColor; [self.layer addSublayer:border]; return border; } 

编辑19/10/15

人们一直在寻求AutoLayouttypes的解决scheme。 在这里你(现在也搬到了Swift):

编辑23/3/17编辑支持Swift 3.0

 func addBorder(edges: UIRectEdge, color: UIColor = UIColor.white, thickness: CGFloat = 1.0) -> [UIView] { var borders = [UIView]() func border() -> UIView { let border = UIView(frame: CGRect.zero) border.backgroundColor = color border.translatesAutoresizingMaskIntoConstraints = false return border } if edges.contains(.top) || edges.contains(.all) { let top = border() addSubview(top) addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "V:|-(0)-[top(==thickness)]", options: [], metrics: ["thickness": thickness], views: ["top": top])) addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "H:|-(0)-[top]-(0)-|", options: [], metrics: nil, views: ["top": top])) borders.append(top) } if edges.contains(.left) || edges.contains(.all) { let left = border() addSubview(left) addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "H:|-(0)-[left(==thickness)]", options: [], metrics: ["thickness": thickness], views: ["left": left])) addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "V:|-(0)-[left]-(0)-|", options: [], metrics: nil, views: ["left": left])) borders.append(left) } if edges.contains(.right) || edges.contains(.all) { let right = border() addSubview(right) addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "H:[right(==thickness)]-(0)-|", options: [], metrics: ["thickness": thickness], views: ["right": right])) addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "V:|-(0)-[right]-(0)-|", options: [], metrics: nil, views: ["right": right])) borders.append(right) } if edges.contains(.bottom) || edges.contains(.all) { let bottom = border() addSubview(bottom) addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "V:[bottom(==thickness)]-(0)-|", options: [], metrics: ["thickness": thickness], views: ["bottom": bottom])) addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "H:|-(0)-[bottom]-(0)-|", options: [], metrics: nil, views: ["bottom": bottom])) borders.append(bottom) } return borders } // Usage: container.addBorder(edges: [.all]) // All with default arguments container.addBorder(edges: [.top], color: UIColor.greenColor()) // Just Top, green, default thickness container.addBorder(edges: [.left, .right, .bottom], color: UIColor.redColor(), thickness: 3) // All except Top, red, thickness 3 

亚当·韦特 ( Adam Waite)的5/5/15版本的快速版:

 extension CALayer { func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) { var border = CALayer() switch edge { case UIRectEdge.Top: border.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), thickness) break case UIRectEdge.Bottom: border.frame = CGRectMake(0, CGRectGetHeight(self.frame) - thickness, CGRectGetWidth(self.frame), thickness) break case UIRectEdge.Left: border.frame = CGRectMake(0, 0, thickness, CGRectGetHeight(self.frame)) break case UIRectEdge.Right: border.frame = CGRectMake(CGRectGetWidth(self.frame) - thickness, 0, thickness, CGRectGetHeight(self.frame)) break default: break } border.backgroundColor = color.CGColor; self.addSublayer(border) } } 

Swift 3:

 extension CALayer { func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) { let border = CALayer() switch edge { case UIRectEdge.top: border.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: thickness) break case UIRectEdge.bottom: border.frame = CGRect.init(x: 0, y: frame.height - thickness, width: frame.width, height: thickness) break case UIRectEdge.left: border.frame = CGRect.init(x: 0, y: 0, width: thickness, height: frame.height) break case UIRectEdge.right: border.frame = CGRect.init(x: frame.width - thickness, y: 0, width: thickness, height: frame.height) break default: break } border.backgroundColor = color.cgColor; self.addSublayer(border) } } 

对我来说最好的办法是在UIView上添加一个类别,但添加视图而不是CALayers,所以我们可以利用AutoresizingMasks来确保边框随着超级视图一起resize。

 - (void)addTopBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth { UIView *border = [UIView new]; border.backgroundColor = color; [border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin]; border.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth); [self addSubview:border]; } - (void)addBottomBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth { UIView *border = [UIView new]; border.backgroundColor = color; [border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth); [self addSubview:border]; } - (void)addLeftBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth { UIView *border = [UIView new]; border.backgroundColor = color; border.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height); [border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin]; [self addSubview:border]; } - (void)addRightBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth { UIView *border = [UIView new]; border.backgroundColor = color; [border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin]; border.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height); [self addSubview:border]; } 

子类UIView并在你的子类中实现drawRect:例如:

 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect)); CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect)); CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor] ); CGContextSetLineWidth(context, 2.0); CGContextStrokePath(context); } 

这将绘制一个2像素红线作为顶部边框。 你所提到的所有其他变化对读者来说都是一个微不足道的工作。

Quartz 2D编程指南推荐使用。

所选答案的代码,以防万一谁想要。

注意:这不适用于自动布局(也就是说,旋转设备到风景等)。

首先定义厚度:

 NSInteger borderThickness = 1; 

然后只要复制使用任何或所有这些来设置您要设置的边框。

顶部的边界

 UIView *topBorder = [UIView new]; topBorder.backgroundColor = [UIColor lightGrayColor]; topBorder.frame = CGRectMake(0, 0, myView.frame.size.width, borderThickness); [myView addSubview:topBorder]; 

底部边界

 UIView *bottomBorder = [UIView new]; bottomBorder.backgroundColor = [UIColor lightGrayColor]; bottomBorder.frame = CGRectMake(0, myView.frame.size.height - borderThickness, myView.frame.size.width, borderThickness); [myView addSubview:bottomBorder]; 

左边界

 UIView *leftBorder = [UIView new]; leftBorder.backgroundColor = [UIColor lightGrayColor]; leftBorder.frame = CGRectMake(0, 0, borderThickness, myView.frame.size.height); [myView addSubview:leftBorder]; 

正确的边界

 UIView *rightBorder = [UIView new]; rightBorder.backgroundColor = [UIColor lightGrayColor]; rightBorder.frame = CGRectMake(myView.frame.size.width - borderThickness, 0, borderThickness, myView.frame.size.height); [myView addSubview:rightBorder]; 

迅速3.0

 extension CALayer { func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) { let border = CALayer(); switch edge { case UIRectEdge.top: border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: thickness) break case UIRectEdge.bottom: border.frame = CGRect(x:0, y:self.frame.height - thickness, width:self.frame.width, height:thickness) break case UIRectEdge.left: border.frame = CGRect(x:0, y:0, width: thickness, height: self.frame.height) break case UIRectEdge.right: border.frame = CGRect(x:self.frame.width - thickness, y: 0, width: thickness, height:self.frame.height) break default: break } border.backgroundColor = color.cgColor; self.addSublayer(border) } } 

Swift版本:

 var myView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100)) myView.backgroundColor = UIColor.yellowColor() var border = CALayer() border.backgroundColor = UIColor.lightGrayColor() border.frame = CGRect(x: 0, y: 0, width: myView.frame.width, height: 0.5) myView.layer.addSublayer(border) 

编辑:更新的版本检查我的回购在这里: https : //github.com/goktugyil/EZSwiftExtensions/blob/master/Sources/UIViewExtensions.swift

看看addBorder的部分

我把Adam Waite和Pauls的答案都拿了下来,并把它们结合起来。 我还添加了将所选边连接在一起的可能性,因此您只需要调用一个如下所示的函数:

 [self.view addBordersToEdge:(UIRectEdgeLeft|UIRectEdgeRight) withColor:[UIColor grayColor] andWidth:1.0]; 

或者:

 [self.view addBordersToEdge:(UIRectEdgeAll) withColor:[UIColor grayColor] andWidth:1.0]; 

你需要实现的是UIView的一个类别,如其他答案中的build议,包含以下实现:

 - (void)addBordersToEdge:(UIRectEdge)edge withColor:(UIColor *)color andWidth:(CGFloat) borderWidth { if (edge & UIRectEdgeTop) { UIView *border = [UIView new]; border.backgroundColor = color; [border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin]; border.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth); [self addSubview:border]; } if (edge & UIRectEdgeLeft) { UIView *border = [UIView new]; border.backgroundColor = color; border.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height); [border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin]; [self addSubview:border]; } if (edge & UIRectEdgeBottom) { UIView *border = [UIView new]; border.backgroundColor = color; [border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth); [self addSubview:border]; } if (edge & UIRectEdgeRight) { UIView *border = [UIView new]; border.backgroundColor = color; [border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin]; border.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height); [self addSubview:border]; } } 

旧的问题,但运行时边界调整的自动布局解决scheme仍然缺less。

 borders(for: [.left, .bottom], width: 2, color: .red) 

下面的UIView扩展将只在给定的边上添加边框。 如果在运行时更改边界,边界将相应地进行调整。

 extension UIView { func borders(for edges:[UIRectEdge], width:CGFloat = 1, color: UIColor = .black) { if edges.contains(.all) { layer.borderWidth = width layer.borderColor = color.cgColor } else { let allSpecificBorders:[UIRectEdge] = [.top, .bottom, .left, .right] for edge in allSpecificBorders { if let v = viewWithTag(Int(edge.rawValue)) { v.removeFromSuperview() } if edges.contains(edge) { let v = UIView() v.tag = Int(edge.rawValue) v.backgroundColor = color v.translatesAutoresizingMaskIntoConstraints = false addSubview(v) var horizontalVisualFormat = "H:" var verticalVisualFormat = "V:" switch edge { case UIRectEdge.bottom: horizontalVisualFormat += "|-(0)-[v]-(0)-|" verticalVisualFormat += "[v(\(width))]-(0)-|" case UIRectEdge.top: horizontalVisualFormat += "|-(0)-[v]-(0)-|" verticalVisualFormat += "|-(0)-[v(\(width))]" case UIRectEdge.left: horizontalVisualFormat += "|-(0)-[v(\(width))]" verticalVisualFormat += "|-(0)-[v]-(0)-|" case UIRectEdge.right: horizontalVisualFormat += "[v(\(width))]-(0)-|" verticalVisualFormat += "|-(0)-[v]-(0)-|" default: break } self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: horizontalVisualFormat, options: .directionLeadingToTrailing, metrics: nil, views: ["v": v])) self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: verticalVisualFormat, options: .directionLeadingToTrailing, metrics: nil, views: ["v": v])) } } } } } 

这是一个简单的解决scheme。 将标签添加到您的UIView,清除标签上的文本,并将标签背景颜色设置为您的边框颜色。 将标签的原点(x,y)设置为视图的原点(x,y)。 并将标签的宽度设置为您的UIView的宽度,将高度设置为1或2(为您的UIView顶部的边框高度)。 这应该是诀窍。

基于NSBum的答案,我采取了类似的方法,并创build了这个简单的UIView子类,以便它在Interface Builder中工作,并与约束: github链接
通过使用CGContextFillRect而不是CGContextStrokePath,我能够预测地保持线条完全固定并在视图范围内。

这是我的博客文章: http : //natrosoft.com/?p=55

– 基本上只需在Interface Builder中放入一个UIView,并将其类types改为NAUIViewWithBorders。
– 然后在你的VC的viewDidLoad中做一些事情:

 /* For a top border only ———————————————- */ self.myBorderView.borderColorTop = [UIColor redColor]; self.myBorderView..borderWidthsAll = 1.0f; /* For borders with different colors and widths ————————— */ self.myBorderView.borderWidths = UIEdgeInsetsMake(2.0, 4.0, 6.0, 8.0); self.myBorderView.borderColorTop = [UIColor blueColor]; self.myBorderView.borderColorRight = [UIColor redColor]; self.myBorderView.borderColorBottom = [UIColor greenColor]; self.myBorderView.borderColorLeft = [UIColor darkGrayColor]; 

这里是直接链接到.m文件,所以你可以看到实现。 还有一个演示项目。 希望这可以帮助别人:)

我的回答类似的问题: https : //stackoverflow.com/a/27141956/435766我个人更喜欢沿着类别的道路上,因为我想能够在UIView的任何子类上使用它。

如果我从故事板内build立,我更喜欢在我有用的UIView后面添加一个UIView …如果我想在我的UIView的顶部创build一个边框,我只是通过我的边框宽度增加背景UIView的高度。 。任何其他方面都可以做到这一点:)

只是张贴在这里帮助某人寻找添加边界。 我在这里接受的答案做了一些改动,这里的swift标签只剩下边框 。 在从CGRectGetHeight(self.frame)CGRectGetWidth(self.frame)的情况下UIRectEdge.BottomUIScreen.mainScreen().bounds.widthCGRectGetWidth(self.frame) UIScreen.mainScreen().bounds.widthCGRectGetWidth(self.frame)以正确获取边框。 使用Swift 2。

最后的延伸是:

 extension CALayer { func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) { let border = CALayer(); switch edge { case UIRectEdge.Top: border.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), thickness); break case UIRectEdge.Bottom: border.frame = CGRectMake(0, CGRectGetHeight(self.frame) - thickness, CGRectGetWidth(self.frame), thickness) break case UIRectEdge.Left: border.frame = CGRectMake(0, 0, thickness, CGRectGetHeight(self.frame)) break case UIRectEdge.Right: border.frame = CGRectMake(CGRectGetWidth(self.frame) - thickness, 0, thickness, CGRectGetHeight(self.frame)) break default: break } border.backgroundColor = color.CGColor; self.addSublayer(border) } } 

就我个人而言,我喜欢view + drawRect的子类,但是这里只是另一种解决方法(它和@If Pollavith接受的答案一样):

您的新边框图层可以设置为您喜欢的任何尺寸。 所以,就像@If Pollavith的回答一样,你创build一个图层,使其像你想要的一样高,并且与你想要的视图一样宽。 使用图层的框架定义将其放置在所需的位置,然后将其作为子图层添加到视图中。

作为参考,我自己的要求是在视图的左手边放一个边框(请不要剪切和粘贴这个代码,只是因为它没有在视图顶部放置边框 – 修改下面的代码很简单):

  CALayer *leftBorder = [CALayer layer]; leftBorder.borderColor = [UIColor colorWithRed:0.0 green:91.0/255.0 blue:141.0/255.0 alpha:1.0].CGColor; leftBorder.borderWidth = 1; leftBorder.frame = CGRectMake(0, 0, 1.0, CGRectGetHeight(self.myTargetView.frame)); [self.myTargetView.layer addSublayer:leftBorder]; 

我认为唯一适中的好处是制作一个小UIView或者UILabel,CALayer应该是“轻量级的”,并且有很多有趣的观点(比如在观点中)关于drawRect和使用CALayers的比较: iOS:使用UIView的“drawRect:”与其图层的delagate“drawLayer:inContext:” )。

Animal451

我喜欢蓝色。

除了n8tr之外, 还可以添加从故事板中设置它们的可用性:
– 在.h文件中添加两个属性,如borderColorborderWidth ;
– 那么你可以添加keyPaths在故事板,请参阅截图的链接

 private class BorderView: UIView { var borderWidth = CGFloat(0) { didSet { setNeedsDisplay() } } var borderColor: UIColor? { didSet { setNeedsDisplay() } } var edges: UIRectEdge = [] { didSet { setNeedsDisplay() } } private override func drawRect(var rect: CGRect) { super.drawRect(rect) if let color = borderColor?.CGColor { let context = UIGraphicsGetCurrentContext() CGContextSetStrokeColorWithColor(context, color) CGContextSetLineWidth(context, borderWidth) let correction = borderWidth / 2 if edges.contains(.All) { rect.origin.x += correction rect.origin.y += correction rect.size.height -= borderWidth rect.size.width -= borderWidth CGContextAddRect(context, rect) } else { if edges.contains(.Top) { CGContextMoveToPoint(context, 0, correction) CGContextAddLineToPoint(context, rect.maxX, correction) } if edges.contains(.Bottom) { CGContextMoveToPoint(context, 0, rect.maxY - correction) CGContextAddLineToPoint(context, rect.maxX, rect.maxY - correction) } if edges.contains(.Left) { CGContextMoveToPoint(context, correction, 0) CGContextAddLineToPoint(context, correction, rect.maxY) } if edges.contains(.Right) { CGContextMoveToPoint(context, rect.maxX - correction, 0) CGContextAddLineToPoint(context, rect.maxX - correction, rect.maxY) } } CGContextStrokePath(context) } } } extension UIView { func addBorder(edges: UIRectEdge = .All, color: UIColor = UIColor.whiteColor(), thickness: CGFloat = 1) { let borderView = BorderView(frame: bounds) borderView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] addSubview(borderView) borderView.userInteractionEnabled = false borderView.borderColor = color borderView.borderWidth = thickness borderView.backgroundColor = UIColor.clearColor() borderView.edges = edges } } 

将DanShev的答案转换成Swift 3

 extension CALayer { func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) { let border = CALayer() switch edge { case .top: border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: thickness) break case .bottom: border.frame = CGRect(x: 0, y: self.frame.height - thickness, width: self.frame.width, height: thickness) break case .left: border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.frame.height) break case .right: border.frame = CGRect(x: self.frame.width - thickness, y: 0, width: thickness, height: self.frame.height) break default: break } border.backgroundColor = color.cgColor; self.addSublayer(border) } } 

对于C#中的Xamarin,我只是在添加子图层时创build内联边框

  View.Layer.AddSublayer(new CALayer() { BackgroundColor = UIColor.Black.CGColor, Frame = new CGRect(0, 0, View.Frame.Width, 0.5f) }); 

您可以按照其他人的build议来安排底部,左侧和右侧的边界。

您也可以检查这个UIKit和基础类别的集合: https : //github.com/leszek-s/LSCategories

它允许用单行代码在UIView的一边添加边框:

 [self.someView lsAddBorderOnEdge:UIRectEdgeTop color:[UIColor blueColor] width:2]; 

它正确处理视图旋转,而大多数在这里发布的答案处理不好。

注意:这里的大部分解决scheme都不适应,不会resize。 resize的解决scheme将对您的启动时间产生巨大的影响,因为它们使用了大量的CPU。

你可以在下面使用这个解决scheme。 它在比层更轻的UIBezierPaths上工作,导致快速启动时间。 它很容易使用,请参阅下面的说明。

 class ResizeBorderView: UIView { var color = UIColor.white var lineWidth: CGFloat = 1 var edges = [UIRectEdge](){ didSet { setNeedsDisplay() } } override func draw(_ rect: CGRect) { if edges.contains(.top) || edges.contains(.all){ let path = UIBezierPath() path.lineWidth = lineWidth color.setStroke() UIColor.blue.setFill() path.move(to: CGPoint(x: 0, y: 0 + lineWidth / 2)) path.addLine(to: CGPoint(x: self.bounds.width, y: 0 + lineWidth / 2)) path.stroke() } if edges.contains(.bottom) || edges.contains(.all){ let path = UIBezierPath() path.lineWidth = lineWidth color.setStroke() UIColor.blue.setFill() path.move(to: CGPoint(x: 0, y: self.bounds.height - lineWidth / 2)) path.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.height - lineWidth / 2)) path.stroke() } if edges.contains(.left) || edges.contains(.all){ let path = UIBezierPath() path.lineWidth = lineWidth color.setStroke() UIColor.blue.setFill() path.move(to: CGPoint(x: 0 + lineWidth / 2, y: 0)) path.addLine(to: CGPoint(x: 0 + lineWidth / 2, y: self.bounds.height)) path.stroke() } if edges.contains(.right) || edges.contains(.all){ let path = UIBezierPath() path.lineWidth = lineWidth color.setStroke() UIColor.blue.setFill() path.move(to: CGPoint(x: self.bounds.width - lineWidth / 2, y: 0)) path.addLine(to: CGPoint(x: self.bounds.width - lineWidth / 2, y: self.bounds.height)) path.stroke() } } } 
  1. 将你的UIView的类设置为ResizeBorderView
  2. 通过在viewDidAppear方法中使用yourview.color和yourview.lineWidth来设置颜色和线宽
  3. 设置边缘,例如:yourview.edges = [.right,.left]([.all])
  4. 享受快速入门和调整边界

如果有人需要Xamarin版本:

 public static class UIUtils { public static void AddBorder(this CALayer cALayer, UIRectEdge edge, UIColor color, float thickness) { var border = new CALayer(); switch (edge) { case UIRectEdge.Top: border.Frame = new CGRect(0, 0, cALayer.Frame.Width, height: thickness); break; case UIRectEdge.Bottom: border.Frame = new CGRect(0, cALayer.Frame.Height - thickness, width: cALayer.Frame.Width, height: thickness); break; case UIRectEdge.Left: border.Frame = new CGRect(0, 0, width: thickness, height: cALayer.Frame.Height); break; case UIRectEdge.Right: border.Frame = new CGRect(cALayer.Frame.Width - thickness, y: 0, width: thickness, height: cALayer.Frame.Height); break; default: break; } border.BackgroundColor = color.CGColor; cALayer.AddSublayer(border); } } 

在viewDidLoad中使用下面的代码

 - (void)viewDidLoad { [super viewDidLoad]; [self.view.layer setBorderWidth: 1.0]; [self.view.layer setCornerRadius:8.0f]; [self.view.layer setMasksToBounds:YES]; [self.view.layer setBorderColor:[[UIColor colorWithRed:251.0f/255.0f green:185.0f/255.0f blue:23.0f/255.0f alpha:1.0f]];` } 

此代码将红色边框设置为您的视图