【发布时间】:2016-03-09 20:23:41
【问题描述】:
我根据这个答案创建了扩展:How to add a border just on the top side of a UIView
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
//https://stackoverflow.com/questions/17355280/how-to-add-a-border-just-on-the-top-side-of-a-uiview
let border = CALayer()
switch edge {
case UIRectEdge.Top:
border.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), thickness)
break
case UIRectEdge.Bottom:
border.frame = CGRectMake(0, CGRectGetHeight(self.frame) - thickness, CGRectGetWidth(self.frame), thickness)
break
case UIRectEdge.Left:
border.frame = CGRectMake(0, 0, thickness, CGRectGetHeight(self.frame))
break
case UIRectEdge.Right:
border.frame = CGRectMake(CGRectGetWidth(self.frame) - thickness, 0, thickness, CGRectGetHeight(self.frame))
break
default:
break
}
border.backgroundColor = color.CGColor;
self.addSublayer(border)
}
}
但在 Ipad 中我只看到 70% 的边框宽度:
我尝试将frame 更改为bounds,但也没有成功。
【问题讨论】:
-
代码对我来说看起来不错,1-您是在模拟器中放大还是缩小?这可能会压缩一些像素,2-您是否还有另一个可以覆盖右侧该部分的白色区域的对象?
-
我在真机上也试过了,一样。但在 iphone 上可以。