如何为UIView添加一个边框线

时间:2016-10-09 04:09:49

标签: ios objective-c uiview

我正在尝试在2个UIView之间添加边界线,正如我研究的那样,以下代码可以添加4个边框。

CGFloat borderWidth = 5.0f;    
self.imgview.frame = CGRectInset(self.imgview. frame, -borderWidth, -borderWidth);
self.imgview. layer.borderColor = [UIColor blueColor].CGColor;
self.imgview. layer.borderWidth = borderWidth;

但是,我只需要添加一个边框,任何建议?提前谢谢。

2 个答案:

答案 0 :(得分:1)

您可以在两个UIView(View1和View2)之间再添加一个UIView(BorderView)。为边框视图指定适当的宽度和背景颜色。enter image description here

答案 1 :(得分:0)

如果要以编程方式设置这些边框,可以使用以下代码段

+(void)SetImageViewBottomBorder :(UIImageView *)imageView{
    CALayer *border = [CALayer layer];
    CGFloat borderWidth = 1;
    border.borderColor = [[self colorWithHexString:@"9B9B9B"] CGColor];
    border.frame = CGRectMake(0, imageView.frame.size.height - borderWidth,    imageView.frame.size.width, imageView.frame.size.height);
    border.borderWidth = borderWidth;
    [imageView.layer addSublayer:border];
    imageView.userInteractionEnabled  = YES;
    imageView.layer.masksToBounds = YES;
}

你可以用UIView替换UIImageView,它就像魅力一样。