iOS 让视图UIView 单独显示某一侧的边框线
有时候需要让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(0, 0, view.frame.size.width, width);
layer.backgroundColor = color.CGColor;
[view.layer addSublayer:layer];
}
if (left) {
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, width, view.frame.size.height);
layer.backgroundColor = color.CGColor;
[view.layer addSublayer:layer];
}
if (bottom) {
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 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, 0, width, view.frame.size.height);
layer.backgroundColor = color.CGColor;
[view.layer addSublayer:layer];
}
}