ios 仅使用 Quartzcore/layer 向 UIView 添加顶部边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17608979/
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-08-31 00:19:49  来源:igfitidea点击:

Add just a top border to an UIView with Quartzcore/layer?

iosuiviewlayer

提问by GilbertOOl

Is it possible to add a border just on top of a UIView, if so, how please?

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

采纳答案by GilbertOOl

i've find solution for me, here's the tricks :

我已经为我找到了解决方案,这是技巧:

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];

回答by Nitin Gohel

I just Testing Bellow few line of Code and it works very nice, just test it in to your Project. hope you'll get your solution easily.

我只是测试波纹管几行代码,它工作得非常好,只需将其测试到您的项目中即可。希望您能轻松获得解决方案。

Why to create new View and adding it into your existing view..? For this task simply create one CALayerand add it into your existing UIView's Layer do as following:-

为什么要创建新视图并将其添加到现有视图中..?对于此任务,只需创建一个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];

}

and It's Output is:-

它的输出是:-

enter image description here

在此处输入图片说明

回答by brandonscript

GilbertOOI's answer in Swift 2:

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)

回答by aroooo

Here's a UIView category that lets you add a layer-back or view-backed border on any side of the UIView: UIView+Borders

这是一个 UIView 类别,可让您在 UIView 的任何一侧添加层背景或视图背景边框:UIView+Borders

回答by dengApro

GilbertOOI's answerin Swift 4:

GilbertOOI在 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)

回答by Andrew Bennett

remus' answer in Obj-C:

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];

回答by n8tr

I created this simple UIView subclass so that it works in Interface Builder and works with constraints: https://github.com/natrosoft/NAUIViewWithBorders

我创建了这个简单的 UIView 子类,以便它在 Interface Builder 中工作并使用约束:https: //github.com/natrosoft/NAUIViewWithBorders

Here's my blog post about it: http://natrosoft.com/?p=55

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

-- Basically just drop in a UIView in Interface Builder and change its class type to NAUIViewWithBorders.
-- Then in your VC's viewDidLoad do something like:

-- 基本上只需在 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];

Here's a direct link to the .m file so you can see the implementation: NAUIViewWithBorders.m
There is a demo project as well.

这是 .m 文件的直接链接,您可以查看实现:NAUIViewWithBorders.m
还有一个演示项目。