有时候需要让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]; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。