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

cmacera
  • 1,140
  • 1
  • 10
  • 21
  • possible duplicate of [Round some corners of UIView and round the view’s layer’s border too](http://stackoverflow.com/questions/14866450/round-some-corners-of-uiview-and-round-the-view-s-layer-s-border-too) – Paresh Navadiya Feb 24 '15 at 10:33
  • Thanks for you suggestion but the post related uses the same code to round the corners, just add a stroke (which I don´t need), and works in the same way: just rounding the top-left corner. – cmacera Feb 24 '15 at 11:00

2 Answers2

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

Community
  • 1
  • 1
Mehul
  • 3,033
  • 23
  • 37
  • 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 Feb 24 '15 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 Feb 25 '15 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 Feb 26 '15 at 05:12
  • @jcmartinac, i have Update my Code , you can see this. – Mehul Feb 26 '15 at 05: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. https://www.dropbox.com/s/w9i3ye2wb2ck0ef/roundcorners.zip?dl=0 – cmacera Feb 26 '15 at 11:03
  • i have Download your Code, & also try to solve your BUG but**i am not able to Create Right Side of TWO corner Round.**...i think you need **Expert Advice**.. i give my best. :) – Mehul Feb 26 '15 at 13:34
  • @jcmartinac, If you find helpful please Upvote the Answer +1. – Mehul Jun 29 '15 at 07:38
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

cmacera
  • 1,140
  • 1
  • 10
  • 21