在自定义的UITableViewCell中,如何仅圆角一个UIView的两个角 - iOS

6

我在一个自定义的UITableViewCell里有一个UIView,我想把这个视图的左下角和右下角圆角化。我尝试了以下代码,但它没有起作用:

- (void)awakeFromNib {
    // Initialization code

    CAShapeLayer * maskLayer = [CAShapeLayer layer];
    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: _viewForTags.bounds byRoundingCorners: UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){7.0, 7.0}].CGPath;

    _viewForTags.layer.mask = maskLayer;
}

我通常在普通的视图控制器中使用 viewWillLayoutSubviews 方法来实现这个功能,它可以完美地工作,但是当我子类化 UITableViewCell 时,就没有这样的方法。

你有什么办法可以在子类化的 UITableViewCell 中将一个视图的两个角变为圆角吗?


1
尝试在cellrowatindexpath中进行一次尝试。 - Anbu.Karthik
尝试过了,不起作用 :( - Hyder
它只被调用了一次的原因是,它不受滚动影响。你能更新你的问题吗? - Anbu.Karthik
如果您想要剪裁图层的内容以及子图层,请不要忘记在CALayer上设置maskToBounds为true。 - Hamish
5个回答

2

实际上,在UITableViewCell中,有一种方法可以做到这一点。那就是layoutSubviews方法。

-(void)layoutSubviews
{
    CAShapeLayer * maskLayer = [CAShapeLayer layer];
    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: _im.bounds byRoundingCorners: UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){7.0, 7.0}].CGPath;

    _im.layer.mask = maskLayer;
}

什么是_im,它是视图吗? - User_1191
这是一个你想要给边角加圆角的视图。 - meth
这是CustomCell类中的一个方法(实际上它是UIView的一个方法,你需要在CustomCell类中重写它)。_im是你在单元格中添加的一个子视图。 - meth
Swift: let maskLayer = CAShapeLayer() maskLayer.path = UIBezierPath(roundedRect: someView.bounds, byRoundingCorners: [UIRectCorner.bottomLeft, UIRectCorner.bottomRight], cornerRadii: CGSize(width: 3.0, height: 3.0)).cgPath someView.layer.mask = maskLayer - Chris Paveglio

1
UITableViewDelegate的
tableView:willDisplayCell:forRowAtIndexPath:

当单元格即将显示在屏幕上时,该方法将被调用。您可以在此方法中编写代码。


谢谢你提供关于Delegate方法的信息,我之前不知道。但是很遗憾,它对我的情况不起作用。只有在我滚动tableView时才会显示圆角,但是当表格第一次加载时并不会。 - Hyder

1
原因是您将代码放错了位置。方法awakeFromNib实际上是视图初始化的地方,在此时,_viewForTags.bounds会给出CGRectZero。您需要将代码移动到setSelected:animated:方法中,或提供具体的CGRect值。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:_viewForTags.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:(CGSize){7.0, 7.0}].CGPath;
    _viewForTags.layer.mask = maskLayer;
}

0
应用于cellforrowatindex。
CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: yourCustomCell.yourViewInCustomCell.bounds byRoundingCorners: UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){7.0, 7.0}].CGPath;

yourCustomCell.yourViewInCustomCell.layer.mask = maskLayer;

0
这段代码适用于Swift 5(以及可能更早的版本):
    let maskLayer = CAShapeLayer()
    maskLayer.path = UIBezierPath(
                         roundedRect:       myView.bounds,
                         byRoundingCorners: [topLeft, .topRight],                             
                         cornerRadii:       CGSize(width:10.0, height:10.0)
                     ).cgPath
    myView.layer.mask = maskLayer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接