且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何制作带有可选圆角和边框的 UIView?

更新时间:2022-11-02 09:20:29

试试下面的代码就行了

Try below code it work

您想要四舍五入 TopLeft 和 TopRight 的视图

   UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];
   [view1 setBackgroundColor:[UIColor grayColor]];
   [self.view addSubview:view1];

设置角落如下代码

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:view1.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(5.0, 5.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
view1.layer.mask = maskLayer;

输出是: