问题描述
如果在视图中使用代码添加视图的边框
If a add a border of a view using code in a view like
self.layer.borderColor = [UIColor yellowColor].CGColor;
self.layer.borderWidth = 2.0f;
边框添加到视图中,如下所示:
the border is added inside the view like the following:
右视图是原始视图,如您所见,有边框视图的黑色区域小于原始视图.但我想要的是原始视图之外的边框,如下所示:.黑色区域等于原来的区域,我该如何实现?
the right view is the original view, as you can see, the black area of bordered view is less than the original one. but what I want to get is a border outside of original view, like this:. the black area is equal to original one, how can I implement it?
推荐答案
不幸的是,不能简单地设置一个小属性来将边框与外部对齐.它绘制到内部对齐,因为 UIViews 默认绘制操作在其边界内绘制.
Unfortunately, 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.
想到的最简单的解决方案是在应用边框时将 UIView 扩展为边框宽度的大小:
The simplest solution that comes to mind would be to expand the UIView by the size of the border width when applying the border:
CGFloat borderWidth = 2.0f;
self.frame = CGRectInset(self.frame, -borderWidth, -borderWidth);
self.layer.borderColor = [UIColor yellowColor].CGColor;
self.layer.borderWidth = borderWidth;
这篇关于在 UIView 外部(而不是内部)添加边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!