ios - 如何仅设置 UIView 的左上角和右上角的cornerRadius?

标签 ios cocoa-touch uiview cornerradius

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

我尝试了以下操作,但最终看不到 View 。

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;

最佳答案

我不确定为什么你的解决方案不起作用,但以下代码对我有用。创建贝塞尔蒙版并将其应用到您的 View 中。在下面的代码中,我将 _backgroundView 的底角圆角化,半径为 3 像素。 self 是自定义的 UITableViewCell:

UIBezierPath *maskPath = [UIBezierPath
    bezierPathWithRoundedRect:self.backgroundImageView.bounds
    byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
    cornerRadii:CGSizeMake(20, 20)
];

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;

self.backgroundImageView.layer.mask = maskLayer;

Swift 2.0 版本进行了一些改进:

let path = UIBezierPath(roundedRect:viewToRound.bounds, byRoundingCorners:[.TopRight, .BottomLeft], cornerRadii: CGSizeMake(20, 20))
let maskLayer = CAShapeLayer()

maskLayer.path = path.CGPath
viewToRound.layer.mask = maskLayer

Swift 3.0 版本:

let path = UIBezierPath(roundedRect:viewToRound.bounds,
                        byRoundingCorners:[.topRight, .bottomLeft],
                        cornerRadii: CGSize(width: 20, height:  20))

let maskLayer = CAShapeLayer()

maskLayer.path = path.cgPath
viewToRound.layer.mask = maskLayer

Swift 扩展 here

关于ios - 如何仅设置 UIView 的左上角和右上角的cornerRadius?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37115499/

相关文章:

ios - 缺少方法声明的上下文

ios - 有人可以解释一下苹果评论指南吗?

ios - NSString 比较 @""== nil

ios - 带有自动布局的 iOS 中的垂直进度条

ios - 创建带标签的居中 View

multithreading - windows phone 7中UI控件的线程亲和性

iphone - 如何在iPhone上放大/放大/缩放UIImageView的特定部分?

ios - 我在 UITableViewController 中覆盖了什么?

iphone - 如何从 UIView 的内容中获取 CGImageRef?

iphone - 对应用程序首选项的更改未保存或无法立即可用?