在UIView外部(而不是内部)添加边框 [英] Add a border outside of a UIView (instead of inside)

查看:211
本文介绍了在UIView外部(而不是内部)添加边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用类似视图的代码在视图中添加边框,则

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外部(而不是内部)添加边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆