如何设置cornerRadius仅UIView的左上角和右上角? [英] how to set cornerRadius for only top-left and top-right corner of a UIView?

查看:596
本文介绍了如何设置cornerRadius仅UIView的左上角和右上角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法只为UIView的左上角和右上角设置cornerRadius?

Is there a way to set cornerRadius for only top-left and top-right corner of a UIView?

编辑:

我试过下面的,但它最终没有看到视图了。下面的代码有什么问题?

I tried the following, but it end up not seeing the view anymore. Anything wrong with the code below?

UIView *view = [[UIView alloc] initWithFrame:frame];

CALayer *layer = [CALayer layer];
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];
layer.shadowPath = shadowPath.CGPath;
view.layer.mask = layer;


推荐答案

Swift代码示例: http://stackoverflow.com/a/35621736/308315

Swift code example here: http://stackoverflow.com/a/35621736/308315

不直接。您必须:


  1. 创建 CAShapeLayer

  2. 基于 view.bounds 将路径设置为 CGPathRef code>,但只有两个圆角(可能使用 + [UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:]

  3. view.layer.mask 设为 CAShapeLayer

  1. Create a CAShapeLayer
  2. Set its path to be a CGPathRef based on view.bounds but with only two rounded corners (probably by using +[UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:])
  3. Set your view.layer.mask to be the CAShapeLayer

这篇关于如何设置cornerRadius仅UIView的左上角和右上角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆