Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add only right border to UIImageView

I want to add a white border to an UIImageView. I tried the following, but it didn't work:

    UIImage *image = [UIImage imageNamed:imagePath];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CGRect rect = imageView.frame;        
    rect.size.width = rect.size.width + 1; // create white right border
    [imageView setBackgroundColor:[UIColor whiteColor]]; 

    imageView.frame = rect;
    [myViewView addSubview:imageView];
like image 217
headkit Avatar asked Jun 06 '12 17:06

headkit


People also ask

How do you add a border to UIImage?

layer. borderWidth = 1; This code can be used for adding UIImageView view border.

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is UIImageView in swift?

An object that displays a single image or a sequence of animated images in your interface.


2 Answers

I now did it like

        CALayer *sublayer = [CALayer layer];
        sublayer.backgroundColor = [UIColor whiteColor].CGColor;
        sublayer.frame = CGRectMake(imageView.bounds.size.width, 0, 1, imageView.frame.size.height);
        [imageView.layer addSublayer:sublayer];
like image 118
headkit Avatar answered Oct 10 '22 07:10

headkit


You can create a UIView that is larger than the UIImage view, and set the image view's frame to be 0,0, width+1, height, and that will add the extra to the side.

like image 24
CBredlow Avatar answered Oct 10 '22 09:10

CBredlow