Button属性maskedCorners
2018-05-10 本文已影响96人
豆豆阳光啊
在iOS 11之前设置button的圆角,使用的方法如下
btn.layer.cornerRadius = 15;
btn.layer.masksToBounds = YES;
//这个方法设置的圆角是针对于四个圆角一起设置,对于个性化UI样式来说不好使用
iOS 11之后button新增了一个属性 maskedCorners
typedef NS_OPTIONS (NSUInteger, CACornerMask)
{
kCALayerMinXMinYCorner = 1U << 0, //左上角
kCALayerMaxXMinYCorner = 1U << 1, //右上角
kCALayerMinXMaxYCorner = 1U << 2, //左下角
kCALayerMaxXMaxYCorner = 1U << 3, //右下角
};
@property CACornerMask maskedCorners
CA_AVAILABLE_STARTING (10.13, 11.0, 11.0, 4.0);
使用方式如下
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 300, 50)];
[self.view addSubview:btn];
btn.backgroundColor = [UIColor redColor];
[btn setTitle:@"button" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
btn.layer.cornerRadius = 15;
btn.layer.maskedCorners = kCALayerMinXMinYCorner;
FCD4ACD1-024D-4415-B9C5-374BB32D1991.png
//四个角
btn.layer.maskedCorners = kCALayerMaxXMinYCorner | kCALayerMaxXMaxYCorner | kCALayerMinXMaxYCorner | kCALayerMinXMinYCorner;
C64D0531-20CA-4AE5-AEE9-591919A03258.png
mark!!!