Setting mask layer on UITableViewCell's subview overrides Auto Layout constraints

后端 未结 2 1757
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 22:03

I have a UITableViewCell with multiple subviews. One of the subviews is a UILabel and the cell\'s height is dynamically sized based on the amount of te

2条回答
  •  死守一世寂寞
    2021-02-04 22:17

    I finally got it. I'm not sure if this is the correct place to put this or if it might cause performance problems, but so far it works perfectly:

    - (void)drawRect:(CGRect)rect
    {
      [super drawRect:rect];
    
      UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.mySubview.bounds
                                                   byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                                         cornerRadii:CGSizeMake(10, 10)];
    
      CAShapeLayer *maskLayer = [CAShapeLayer layer];
      maskLayer.frame = self.mySubview.bounds;
      maskLayer.path = maskPath.CGPath;
      self.mySubview.layer.mask = maskLayer;
    
    }
    

    I had to override drawRect: in my UITableViewCell subclass and set the mask layer there.

提交回复
热议问题