以按钮为例 其他的也一样
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 160, 40);
[btn setBackgroundColor:[UIColor cyanColor]];
[btn setTitle:@"这是一个按钮" forState:UIControlStateNormal];
[self.view addSubview:btn];
/**
* 设置圆角
*/
CGSize radio = CGSizeMake(5, 5);//圆角尺寸
UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:btn.bounds byRoundingCorners:corner cornerRadii:radio];//这地方只能有bounds 使用frame 不可以
CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//创建shapelayer
masklayer.frame = btn.bounds;
masklayer.path = path.CGPath;//设置路径
btn.layer.mask = masklayer;
网友评论