IOS设置按钮为圆角的示例代码

时间:2021-05-20

iOS中很多时候都需要用到指定风格的圆角按钮,以下是UIButton提供的创建圆角按钮方法

设置按钮的4个角:

左上:UIRectCornerTopLeft

左下:UIRectCornerBottomLeft

右上:UIRectCornerTopRight

右下:UIRectCornerBottomRight

示例代码:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 60, 80, 40)];button.backgroundColor = [UIColor orangeColor];[self.view addSubview:button]; //这里设置的是左上和左下角UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(8, 8)];CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];maskLayer.frame = button.bounds;maskLayer.path = maskPath.CGPath;button.layer.mask = maskLayer;

效果图

总结

IOS设置按钮为圆角的方法到这就结束了,大家都学会了吗?希望这篇文章对大家的开发能有一定的帮助,如果有疑问大家可以留言交流。谢谢大家对的支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章