0

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

2

2 Answers 2

1

@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

7
  • Thanks for helping! Unfortunately it works the same way before... Testing the code, I have realized that only works the top-left corner, if I try to round any other it does nothing. I am totally lost
    – cmacera
    Commented Feb 24, 2015 at 14:09
  • I have seen your code works inside uiviewcontroller, but I am using uitableviewcontroller and is when it does not work. Did you try it in a uiimageview inside a cell in a uitableview? I think the problem is in uitableview
    – cmacera
    Commented Feb 25, 2015 at 11:42
  • Yes, its working good in UITableView.. cell.Img_thumb=(UIImageView *)[self roundCornersOnView: cell.Img_thumb onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:20.0];
    – Mehul
    Commented Feb 26, 2015 at 5:12
  • @jcmartinac, i have Update my Code , you can see this.
    – Mehul
    Commented Feb 26, 2015 at 5:17
  • Hello and thanks again! I keep looking for solutions and have decided to attach a simple project to see clearly my problem by rounding the top corners of a UIImageView that occupies the entire width of a cell in a table. When I apply the code to do this, the image loses the screen width and it does not work for me. With this example I hope someone can help me. dropbox.com/s/w9i3ye2wb2ck0ef/roundcorners.zip?dl=0
    – cmacera
    Commented Feb 26, 2015 at 11:03
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.