美文网首页
iOS 设置UIView的某个角圆角

iOS 设置UIView的某个角圆角

作者: 码哥进化 | 来源:发表于2017-11-16 22:51 被阅读447次

    1、设置四个角为同一弧度的圆角

    UIView *cornerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 10)];
    cornerView.backgroundColor = [UIColor whiteColor];
    cornerView.layer.masksToBounds = YES;
    cornerView.layer.cornerRadius = 8;
    [self addSubview:cornerView];
    

    2、设置某个角为圆角

    UIRectCornerTopLeft  //上部左角
    UIRectCornerTopRight  //上部右角
    UIRectCornerBottomLeft  //下部左角
    UIRectCornerBottomRight //下部右角
    UIRectCornerAllCorners  // 设置所有的角
    可以通过 “|” 组合使用
    
    UIView *cornerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,  120, 10)];
    cornerView.backgroundColor = [UIColor redColor];
    [self addSubview:cornerView];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cornerView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = cornerView.bounds;
    maskLayer.path = maskPath.CGPath;
    cornerView.layer.mask = maskLayer;
    

    相关文章

      网友评论

          本文标题:iOS 设置UIView的某个角圆角

          本文链接:https://www.haomeiwen.com/subject/utzrvxtx.html