关键代码如下:
self.exitBtn.layer.masksToBounds = YES;
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.exitBtn.bounds byRoundingCorners: UIRectCornerTopRight cornerRadii: (CGSize){8.0f, 8.0f}].CGPath;
self.exitBtn.layer.mask = maskLayer;
其中UIRectCornerTopRight即为设置圆角的位置:上右,同时我们可以看到苹果API中的位移枚举如下:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
故我们可以随心所欲地设置任意控件任意位置的圆角。ps:8.0f为设置圆角大小。
iOS控件圆角自定义
本文介绍如何使用Swift在iOS应用中为按钮等控件设置特定位置的圆角效果,并提供了一段示例代码来展示如何仅设置按钮右上角的圆角。
247

被折叠的 条评论
为什么被折叠?



