2

I want to use round corner style in my UIView, and here is my code:

UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:self.styleView1.bounds
                                               byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                                     cornerRadii:CGSizeMake(4, 4)];
CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
maskLayer1.frame = self.styleView1.bounds;
maskLayer1.path = maskPath1.CGPath;
self.styleView1.layer.borderWidth = 1;
[self.styleView1.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
self.styleView1.layer.mask = maskLayer1;

The effect is like that: enter image description here

There is blank on the corner, like feather effect in Photoshop.

But what I want is this: enter image description here

How to make it happen?

Don_Chen
  • 987
  • 2
  • 8
  • 16
  • I just want to set `TopLeft` and `TopRight` to round corners so perhaps these 2 threads not suit for my question. – Don_Chen Jan 20 '15 at 05:03
  • @Don_Chen try this (http://stackoverflow.com/questions/25616382/how-to-set-cornerradius-for-only-bottom-left-bottom-right-and-top-left-corner-of) and (http://stackoverflow.com/questions/10167266/how-to-set-cornerradius-for-only-top-left-and-top-right-corner-of-a-uiview) – ChintaN -Maddy- Ramani Jan 20 '15 at 05:06

2 Answers2

4

If self is a UIViewController or UISplitViewController then self doesn't have bounds, it's a controller.

Try this one:

CGRect bounds = self.view.bounds;
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.styleView1.bounds
                                 byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                       cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
self.styleView1.layer.mask = maskLayer;
Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
  • If this answer is satisfy you, then please accept my answer. So that another developer will follow this :) – Soumya Ranjan Jan 20 '15 at 04:54
  • But I just want to set the `TopLeft` and `TopRight` to round corner, not all 4 corners. – Don_Chen Jan 20 '15 at 05:00
  • But your question was "How to change the style of UIView to round corner?". You are not mention for top both corner – Soumya Ranjan Jan 20 '15 at 05:02
  • I update my answer. Please check once. It's working perfect for me. please check – Soumya Ranjan Jan 20 '15 at 05:20
  • Thanks for your answer. I just found the solution [here](http://stackoverflow.com/questions/25616382/how-to-set-cornerradius-for-only-bottom-left-bottom-right-and-top-left-corner-of). Adding **another** CAShapeLayer can solve it. – Don_Chen Jan 20 '15 at 06:00
1

You can set radius for top view as below code check (how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?) for reference.

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:yourView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.frame = yourView.bounds;
shapeLayer.path  = path.CGPath;
yourView.layer.mask = shapeLayer;

and you will get following.enter image description here

Community
  • 1
  • 1
ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
  • Thanks. I followed the [link](http://stackoverflow.com/questions/25616382/how-to-set-cornerradius-for-only-bottom-left-bottom-right-and-top-left-corner-of) you've commented and finally found the answer. The magic is and **another** CAShapeLayer as @Justin Boo said. – Don_Chen Jan 20 '15 at 05:57