美文网首页iOS开发
iOS开发 cell设置圆角无效

iOS开发 cell设置圆角无效

作者: 夜凉听风雨 | 来源:发表于2019-10-14 10:03 被阅读0次

更新一下: IOS14系统 cell不能直接设置圆角,只能设置cell.contentView的圆角!!!

通常用下面的代码为cell设置圆角,但是可能依然没有效果

        cell.layer.cornerRadius = 6;
        cell.layer.masksToBounds = YES;

换为下面这种方式生效。

        cell.layer.cornerRadius = 6;
        cell.clipsToBounds = YES;

如果上面的方式依然不生效,可能是你打开了cell左滑操作的功能。

在cell.m文件的中写如下代码

- (void)layoutSubviews {
    [super layoutSubviews];
    self.layer.cornerRadius = k6sWidth(6);
    self.clipsToBounds = YES;
}

效果如下图:


图片.png

我们发现右边的删除按钮是没有圆角的。

通过层级图我们发现,左滑cell显示删除按钮时,iOS13以上和以下的系统视图是不一样的,IOS13以下系统,cell右边出现一个和它平级的视图UISwipeActionPullView ,而IOS13以上系统cell出现了一个父视图_UITableViewCellSwipeContainerView,它的子视图有UISwipeActionPullView。

iOS13以下层级图:

图片.png 图片.png

iOS13以上层级图:


图片.png 图片.png

如果想要右边的删除按钮右边也有圆角效果 我们只要找到这个父视图,设置它的圆角就可以了。

代码如下:

- (void)layoutSubviews {
    [super layoutSubviews];
    self.layer.cornerRadius = 6;
    self.clipsToBounds = YES;
    [self dealDeleteButton];
}

- (void)dealDeleteButton {
    UIView *superView = self.superview;
    if ([superView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")]) {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:superView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight  cornerRadii:CGSizeMake(6, 6)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = superView.bounds;
        maskLayer.path = maskPath.CGPath;
        superView.layer.mask = maskLayer;
    }
}

效果如下:

图片.png

相关文章

网友评论

    本文标题:iOS开发 cell设置圆角无效

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