简体   繁体   中英

How to add one border line for UIView

I am trying to add border line between 2 UIView, as i researched, the following code can add 4 borders.

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;

However, i just need to add one border, any advice? Thanks in advance.

You can add one One more UIView (BorderView) between two UIView (View1 and View2). Give the border view appropriate width and background color. 在此处输入图片说明

If you want to set these borders programmatically, you can use the following snippet of code

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

you can replace the UIImageView with UIView and it will be working like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM