Hanrovey Blog

编程是一门艺术,我在追寻艺术的路上...

【iOS开发】自定义UIView边框的颜色

开发中经常要修改UIView某一个边框的颜色,特别是UITableView。核心代码如下:

- (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];
    }

}
示例代码:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(150, 250, 100, 100)];

[self setBorderWithView:view top:YES left:YES bottom:NO right:YES borderColor:[UIColor greenColor] borderWidth:4.0];

[self.view addSubview:view];

效果图:

这里写图片描述

备注:可以支持( 上 下 左 右 )任一个边框颜色的修改

阅读更多
版权声明:转载请注明出处。 https://blog.csdn.net/Hanrovey/article/details/51536789
个人分类: Objective-C
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

【iOS开发】自定义UIView边框的颜色

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭