iOS 设置UI控件圆角

一: 设置UIView上方圆角或者下方圆角

  UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.contentView.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(4, 4)];
  CAShapeLayer * maskLayer = [[CAShapeLayer alloc]init];
  maskLayer.frame = self.contentView.bounds;
  maskLayer.path = maskPath.CGPath;
  self.contentView.layer.mask = maskLayer;

//设置UITableView section 圆角

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(tintColor)]) {

    CGFloat cornerRadius = 10.f;
    cell.backgroundColor = UIColor.clearColor;
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGRect bounds = CGRectInset(cell.bounds, 15, 0);
    BOOL addLine = NO;
    if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
        CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
        
    } else if (indexPath.row == 0) {
        
        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
        addLine = YES;
        
    } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
    } else {
        CGPathAddRect(pathRef, nil, bounds);
        addLine = YES;
    }
    layer.path = pathRef;
    CFRelease(pathRef);
    //颜色修改[UIColor colorWithWhite:1.f alpha:0.5f].CGColor
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor=[UIColor clearColor].CGColor;
    if (addLine == YES) {
        CALayer *lineLayer = [[CALayer alloc] init];
        CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
        lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
        //tableView.separatorColor.CGColor
        lineLayer.backgroundColor = [UIColor clearColor].CGColor;
        [layer addSublayer:lineLayer];
    }
    UIView *testView = [[UIView alloc] initWithFrame:bounds];
    [testView.layer insertSublayer:layer atIndex:0];
    testView.backgroundColor = UIColor.clearColor;
    cell.backgroundView = testView;
  }
//    }
}

设置好以后就这样了


屏幕快照 2017-12-22 11.16.32.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,135评论 1 32
  • 废话不多说,直接上干货 ---------------------------------------------...
    小小赵纸农阅读 3,415评论 0 15
  • 恨不能生无七情五感 不闻不言不欲看 真乱 懂不了不想懂 都去他的吧 最好不相关
    意莫安阅读 152评论 2 1
  • 崇尚平行世界,追求多元生活。 ------爱读书的高博士 继续5:30起床。5:50左右,一群来自天南海北打了鸡血...
    Jean_zz阅读 564评论 0 3
  • (一) 溪水很笨 只在同一块石头刻舟求剑 当做你还没走 (二) 天上有一二三只鸟 不是舌头在清点 是猎枪在数 (三...
    米兰的小石匠阅读 2,471评论 67 87