简体   繁体   English

如何在uiview中添加边框?

[英]How to add a border inside a uiview?

I am having a uiview and I would like to add a border iside this UIVIew approximately 75 % of UIView.我有一个 uiview,我想在这个 UIVIew 旁边添加一个边框,大约是 UIView 的 75%。 Could any one help with this.任何人都可以帮助解决这个问题。 I could get the solution to draw the border outside.我可以得到在外面绘制边界的解决方案。

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;

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM