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

后端 未结 2 1748
隐瞒了意图╮
隐瞒了意图╮ 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.

    0 讨论(0)
  • 2021-02-04 22:26

    I have had same problem and doing things in - (void)drawRect:(CGRect)rect does the work but it could be costly in terms of performance.

    You can call your clipping or shadow methods in perform selector

    [self performSelector:@selector(<your drawing method>) withObject:nil afterDelay:0.0000001 ];//a very low delay
    
    0 讨论(0)
提交回复
热议问题