ios - 在 UIView 外部添加边框(而不是在内部)

标签 ios uiview border

如果在像这样的 View 中使用代码添加 View 的边框

self.layer.borderColor = [UIColor yellowColor].CGColor;
self.layer.borderWidth = 2.0f;

边框被添加到 View 中,如下所示: enter image description here

右 View 是原始 View ,可以看到,边框 View 的黑色区域小于原始 View 。但我想要得到的是原始 View 之外的边框,如下所示:enter image description here .黑色区域与原始区域相等,如何实现?

最佳答案

不幸的是,您不能简单地设置一个小属性来将边框与外部对齐。它向内对齐绘制,因为 UIViews 默认绘图操作在其边界内绘制。

想到的最简单的解决方案是在应用边框时将 UIView 扩展边框宽度的大小:

CGFloat borderWidth = 2.0f;

self.frame = CGRectInset(self.frame, -borderWidth, -borderWidth);
self.layer.borderColor = [UIColor yellowColor].CGColor;
self.layer.borderWidth = borderWidth;

关于ios - 在 UIView 外部添加边框(而不是在内部),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15184133/

相关文章:

ios - 推送到详细 View

ios - 添加延迟动画时,UIView 动画不起作用

java - 更改 AWT TextField 的边框颜色

html - 为什么即使未应用边框底部也会出现?

ios - Apple 的指南,关于网络通信、加密数据

ios - Xcode 5 不需要 Quartz Core Import?

iphone - 没有年份的NSDateFormatterFullStyle(NSDateFormatter)

ios - 为什么我的 IBDesignable 不允许我使用 setImage : on my custom UIButton?

ios - 添加 UIImageView 并处理大小/适合

css - 如何创建带有间隙的自定义边框?