一:首先给UIView添加一个扩展,加入添加圆角的方法
OC实现
//顶部所有圆角
- (void)drawTopCornerRadius:(NSInteger)radius {
// 任意圆角
CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(radius, radius)].CGPath;
CAShapeLayer *lay = [CAShapeLayer layer];
lay.path = path;
self.layer.mask = lay;
}
// 顶部所有圆角
- (void)drawBottomCornerRadius:(NSInteger)radius {
// 任意圆角
CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(radius, radius)].CGPath;
CAShapeLayer *lay = [CAShapeLayer layer];
lay.path = path;
self.layer.mask = lay;
}
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
Corners可以选择设置对应的哪个圆角
二:在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 方法实现,添加圆角的逻辑
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) { // 第一行顶部添加
[cell drawTopCornerRadius:20];
} else if(indexPath.row == 最后一行) { //最后一行底部添加
[cell drawBottomCornerRadius:20];
}
}
如图:就这样完美的设置好啦