有时候需要让view显示某一侧的边框线,这时设置layer的border是达不到效果的。在网上查阅资料发现有一个投机取巧的办法,原理是给view的layer再添加一个layer,让这个layer充当边框线的角色。根据这个方法我写了一个函数,有需要的朋友可以拿去试下。
- (void)setBorderWithView:(UIView *)view top:(BOOL)top left:(BOOL)left bottom:(BOOL)bottom right:(BOOL)right borderColor:(UIColor *)color borderWidth:(CGFloat)width
{
if (top) {
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(, , view.frame.size.width, width);
layer.backgroundColor = color.CGColor;
[view.layer addSublayer:layer];
}
if (left) {
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(, , width, view.frame.size.height);
layer.backgroundColor = color.CGColor;
[view.layer addSublayer:layer];
}
if (bottom) {
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(, view.frame.size.height - width, view.frame.size.width, width);
layer.backgroundColor = color.CGColor;
[view.layer addSublayer:layer];
}
if (right) {
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(view.frame.size.width - width, , width, view.frame.size.height);
layer.backgroundColor = color.CGColor;
[view.layer addSublayer:layer];
}
}
本文出自 “rainbownight” 博客,请务必保留此出处http://rainbownight.blog.51cto.com/1336585/1332286