如何将 UIView 的左上角和右上角改为圆角?

How to change the TopLeft and TopRight corners of UIView to round corner?

我想在我的 UIView 中使用圆角样式,这是我的代码:

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;

效果是这样的:

边角有空白,像Photoshop中的羽化效果

但我想要的是:

如何实现?

如果 self 是 UIViewController 或 UISplitViewController,那么 self 没有边界,它是一个控制器。

试试这个:

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;

您可以设置顶视图的半径,如下代码检查(how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?)以供参考。

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;

你会得到关注。