xcode 如何在uiview中添加边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28824950/
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 add a border inside a uiview?
提问by Ashwin P
I am having a uiview and I would like to add a border iside this UIVIew approximately 75 % of UIView. Could any one help with this. I could get the solution to draw the border outside.
我有一个 uiview,我想在这个 UIVIew 旁边添加一个边框,大约是 UIView 的 75%。任何人都可以帮助解决这个问题。我可以得到在外面绘制边界的解决方案。
回答by aroragourav
Well there isn't simply a little property you can set to align the border to the outside. It draws aligned to the inside because the UIViews default drawing operations draw within its bounds.
好吧,不是简单的一个小属性可以设置来将边框与外部对齐。它绘制到内部对齐,因为 UIViews 默认绘制操作在其边界内绘制。
The simplest solution that comes to mind would be to expand the UIView by the size of the border width when applying the border:
想到的最简单的解决方案是在应用边框时按边框宽度的大小扩展 UIView:
CGFloat borderWidth = 2.0f;
self.frame = CGRectInset(self.frame, -borderWidth, -borderWidth);
self.layer.borderColor = [UIColor yellowColor].CGColor;
self.layer.borderWidth = borderWidth;
回答by James Zaghini
@aroragourav's answer for Swift 3+
@aroragourav 对 Swift 3+ 的回答
let borderWidth: CGFloat = 2
frame = frame.insetBy(dx: -borderWidth, dy: -borderWidth)
layer.borderColor = UIColor.yellow.cgColor
layer.borderWidth = borderWidth