【问题标题】:Round only 2 corners of a UIView in custom UITableViewCell - iOS在自定义 UITableViewCell - iOS 中仅圆化 UIView 的 2 个角
【发布时间】:2016-05-05 18:06:41
【问题描述】:

我在自定义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 中将视图的 2 个角取圆吗?

【问题讨论】:

  • 一次在 cellrowatindexpath 中尝试并尝试
  • 试过了。不工作:(
  • 它只调用一次的原因,如果你滚动它不影响,你能更新你的问题
  • 如果你想让它剪切图层的内容以及子图层,不要忘记将CALayer上的maskToBounds设置为true

标签: ios objective-c uitableview rounded-corners cornerradius


【解决方案1】:

UITableViewDelgate 的

tableView:willDisplayCell:forRowAtIndexPath:

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

【讨论】:

  • 感谢您提供有关我不知道的委托方法的信息。但不幸的是,它不适用于我的情况。它仅在我滚动 tableView 时才会显示,然后显示圆角,但在第一次加载表格时不会显示
【解决方案2】:

在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;

【讨论】:

    【解决方案3】:

    原因是您将代码放在错误的位置。方法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;
    }
    

    【讨论】:

      【解决方案4】:

      实际上在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,是view吗?
      • 这是一个要圆角的视图。
      • 这是customcell类里面的一个方法(实际上是一个UIView的方法,你需要在CustomCell类里面重写它)。 _im 是您在单元格中添加的子视图。
      • 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
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      相关资源
      最近更新 更多