博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发设置View某个角为圆角
阅读量:6398 次
发布时间:2019-06-23

本文共 4249 字,大约阅读时间需要 14 分钟。

需要借助UIBezierPath与CAShapeLayer进行实现

 

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;

 

 

通过枚举类型UIRectCorner选择上下左右

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {

            UIRectCornerTopLeft = 1 << 0,
            UIRectCornerTopRight = 1 << 1,
            UIRectCornerBottomLeft = 1 << 2,
            UIRectCornerBottomRight = 1 << 3,
            UIRectCornerAllCorners = ~0UL
            
        };

 

UILabel设置圆角可能出现问题

如果Uilabel设置圆角,在分辨率高的手机可能出现一条细线,因为label的宽度不是整数,取整数即可。

 

 

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer;
作者:薄凉_简书
链接:https://www.jianshu.com/p/867124573ab0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer;
作者:薄凉_简书
链接:https://www.jianshu.com/p/867124573ab0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer;
作者:薄凉_简书
链接:https://www.jianshu.com/p/867124573ab0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer;
作者:薄凉_简书
链接:https://www.jianshu.com/p/867124573ab0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer;
作者:薄凉_简书
链接:https://www.jianshu.com/p/867124573ab0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer;
作者:薄凉_简书
链接:https://www.jianshu.com/p/867124573ab0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

转载于:https://www.cnblogs.com/Yishu/p/9570528.html

你可能感兴趣的文章
C# DES加密
查看>>
浅谈Oracle分区表之范围分区
查看>>
IBM Tivoli NetView网管软件实战
查看>>
IPSec逻辑体系架构
查看>>
Exchange 2013部署系列之(六)配置邮件流和客户端访问
查看>>
List of Free Programming books
查看>>
思考Android架构(二):像Android框架,如何(How-to)吸引开发者来使用它呢?
查看>>
在html中,怎么获取当前页面body的高度,body是没有设置高度的,但是里面有内容...
查看>>
把 Array 转换成 Map
查看>>
MyBatis入门学习
查看>>
ASA防火墙IPSEC
查看>>
djangostart01
查看>>
Ubuntu 12.04无法关机、重启解决办法
查看>>
Tomcat的四种基于HTTP协议的Connector性能比较
查看>>
【后缀数组】
查看>>
图片缩放裁剪
查看>>
jquery ajax 回调函数的值alert出来[object Object] 解决方法
查看>>
JQuery选择器总结
查看>>
MySQL安装详解(V5.5 For Windows)
查看>>
Android单例模式
查看>>