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

时间:2015-01-20 04:47:02

标签: ios uiview

我想在我的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;

效果是这样的: enter image description here

角落里有空白,就像Photoshop中的羽毛效果一样。

但我想要的是: enter image description here

如何实现目标?

2 个答案:

答案 0 :(得分:4)

如果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;

答案 1 :(得分:1)

您可以为顶视图设置半径,如下面的代码检查(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;

您将获得关注。enter image description here