cmacera
cmacera

Reputation: 1130

Selective round corners in image view - iOS

I am working in Xcode Version 6.1.1 and iOS 8.1 to develop my app in which I need round top-left and top-right corners in an image view according to design.

I have used the following code before, and it works correctly in previous versions of Xcode:

UIImageView *locationImage = (UIImageView *)[cell viewWithTag:101];

UIBezierPath *maskPath1;

maskPath1 = [UIBezierPath bezierPathWithRoundedRect:locationImage.bounds
                                  byRoundingCorners:(UIRectCornerTopRight | UIRectCornerTopLeft)
                                        cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
maskLayer1.frame = locationImage.bounds;
maskLayer1.path = maskPath1.CGPath;
locationImage.layer.mask = maskLayer1;

Now I get the top-left corner rounded but not the right one. I know the code is correct because if I apply it to a not constrained image view, it works well, but I need to constrain the items to the view. I use auto layout.

link to image: https://www.dropbox.com/s/orisd8gzbdhsr4z/round-corners.tiff?dl=0

There is something I am doing wrong? How can I round two corners properly?

Thanks in advance

*Sorry for my english

Upvotes: 0

Views: 849

Answers (2)

cmacera
cmacera

Reputation: 1130

Finally I have found a solution nesting the uiimageview in a uiview with corner radius set in the User Defined Runtime Attributes and setting the cliptobounds of this uiview to yes.

If anyone needs further explanation, I will give delighted

Upvotes: 0

Mehul
Mehul

Reputation: 3178

@jcmartinac,

you can use this Solution -how to set cornerRadius for only top-left and top-right corner of a UIView?

in your Code apply this Code , i have tested , its working perfectly.

maskPath1 = (UIImageView *)[self roundCornersOnView: maskPath1 onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:20.0];

//use below Method to Set Corner Radius Round..

-(UIView *)roundCornersOnView:(UIView *)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius {

    if (tl || tr || bl || br) {
        UIRectCorner corner = 0; //holds the corner
        //Determine which corner(s) should be changed
        if (tl) {
            corner = corner | UIRectCornerTopLeft;
        }
        if (tr) {
            corner = corner | UIRectCornerTopRight;
        }
        if (bl) {
            corner = corner | UIRectCornerBottomLeft;
        }
        if (br) {
            corner = corner | UIRectCornerBottomRight;
        }

        UIView *roundedView = view;
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = roundedView.bounds;
        maskLayer.path = maskPath.CGPath;
        roundedView.layer.mask = maskLayer;
        return roundedView;
    } else {
        return view;
    }

}

///////////// For TableVIew Use Below Code in cellForRowAtIndexPath Method /////

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.Img_thumb=(UIImageView *)[self roundCornersOnView: cell.Img_thumb onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:20.0];
}

Check Screen Shot :-> Effect on Table

Upvotes: 1

Related Questions