如何使用Quartzcore/layer为UIView添加顶部边框?

38

是否可以在UIView顶部添加边框,如果可以,请问如何实现?


你可以设置view.layer.bordercolor和borderwidth,bordercolor。 - rakeshNS
不好意思,您的解决方案为所有视图添加了边框,而不仅仅是UIView的顶部!!! - GilbertOOl
1
我们可以在UIView的一侧设置边框,这篇文章可能会有所帮助https://dev59.com/n2gu5IYBdhLWcg3wkHyP。 - rakeshNS
可能是UIView底部边框?的重复问题。 - Cœur
尝试一下这个 https://dev59.com/Y2Qm5IYBdhLWcg3wxBRg#48293635 - Datt Patel
8个回答

98

我刚测试了下面的几行代码,它很好用,只需要将其测试到您的项目中。希望您能轻松地找到解决方案。

为什么要创建新视图并将其添加到现有视图中?对于这个任务,只需创建一个CALayer并将其添加到您现有UIView的层中,按照以下步骤操作:

#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad
{
    CALayer *TopBorder = [CALayer layer];
    TopBorder.frame = CGRectMake(0.0f, 0.0f, myview.frame.size.width, 3.0f);
    TopBorder.backgroundColor = [UIColor redColor].CGColor;
    [myview.layer addSublayer:TopBorder];

  [super viewDidLoad];

}

并且它的输出为:

enter image description here


2
再说一点,你的解决方案而不是我的解决方案在自动旋转方面表现不佳! - GilbertOOl
2
一个UIView至少可以同样好地完成这个任务。 - Pascalius
1
不,你可以使用UIView _完全_像这样做。 - Pascalius
当视图改变大小(例如动画)时,边框没有被重新调整大小。 - Rafał Sroka

18

我已经为自己找到了解决方案,以下是诀窍:

CGSize mainViewSize = self.view.bounds.size;
CGFloat borderWidth = 1;
UIColor *borderColor = [UIColor colorWithRed:37.0/255 green:38.0/255 blue:39.0/255 alpha:1.0];
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, mainViewSize.width, borderWidth)];
topView.opaque = YES;
topView.backgroundColor = borderColor;
topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;        
[self.view addSubview:topView];

2
如果您需要担心旋转,这个非常棒。 - werm098

10

GilbertOOI 在 Swift 2 中的回答:

let topBorder: CALayer = CALayer()
topBorder.frame = CGRectMake(0.0, 0.0, myView.frame.size.width, 3.0)
topBorder.backgroundColor = UIColor.redColor().CGColor
myView.layer.addSublayer(topBorder)

6

这是一个针对任何UIView的类别,可以让你在UIView的任何一侧添加层或视图支持的边框:UIView+Borders


5

GilbertOOI's answer in Swift 4:

let topBorder: CALayer = CALayer()
topBorder.frame = CGRect(x: 0, y: 0, width: myView.frame.size.width, height: 1)
topBorder.backgroundColor = UIColor.purple.cgColor
myView.layer.addSublayer(topBorder)

2
我创建了这个简单的UIView子类,它可以在Interface Builder中工作并且可以与约束一起使用:https://github.com/natrosoft/NAUIViewWithBorders 这是我的博客文章: http://natrosoft.com/?p=55 -- 基本上只需要在Interface Builder中放置一个UIView并将其类类型更改为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文件的链接,你可以查看实现:NAUIViewWithBorders.m。同时还有一个演示项目。

1
谢谢这个 -- 我发现它非常稳定且易于使用,实现也非常简单。 - weienw
2
最好使用扩展而不是子类化。 - Edward Anthony

2

Remus在Obj-C中的回答:

 CALayer *topBorder = [CALayer new];
 topBorder.frame = CGRectMake(0.0, 0.0, self.frame.size.width, 3.0);
 topBorder.backgroundColor = [UIColor redColor].CGColor;
 [myView.layer addSublayer:topBorder];

0

Swift5:

我们将编写一个单独的方法来为此视图添加边框。要向此视图添加边框,我们将创建两个具有所需厚度的图层。我们将把这两个图层的框架设置为视图的顶部和底部。我们将在这些图层上设置所需的边框背景颜色,并将这些图层作为子层添加到视图中。

func addTopBorders() {
   let thickness: CGFloat = 1.0
   let topBorder = CALayer()
   topBorder.frame = CGRect(x: 0.0, y: 0.0, width: 
   self.down_view_outlet.frame.size.width, height: thickness)
   topBorder.backgroundColor = UIColor.white.cgColor
   down_view_outlet.layer.addSublayer(topBorder)
}

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