【问题标题】:How to change the TopLeft and TopRight corners of UIView to round corner? [duplicate]如何将 UIView 的 TopLeft 和 TopRight 角更改为圆角? [复制]
【发布时间】:2015-03-18 05:50:00
【问题描述】:

我想在我的 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中的羽毛效果。

但我想要的是:

如何实现?

【问题讨论】:

标签: ios uiview


【解决方案1】:

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

【讨论】:

  • 如果这个答案让你满意,那么请接受我的回答。这样另一个开发人员会遵循这个:)
  • 但我只想将 TopLeftTopRight 设置为圆角,而不是全部 4 个角。
  • 但是您的问题是“如何将 UIView 的样式更改为圆角?”。你没有提到两个角落
  • 我更新了我的答案。请检查一次。它对我来说很完美。请检查
  • 感谢您的回答。我刚刚找到了解决方案here。添加另一个 CAShapeLayer就可以解决了。
【解决方案2】:

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

你会得到关注。

【讨论】:

  • 谢谢。我关注了你评论的link,终于找到了答案。正如@Justin Boo 所说,魔法是另一个 CAShapeLayer。
猜你喜欢
  • 2020-02-25
  • 2011-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多