skip to Main Content

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?

2

Answers


  1. 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;
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search