在UITableViewCell的子视图上设置遮罩层会覆盖自动布局约束 [英] Setting mask layer on UITableViewCell's subview overrides Auto Layout constraints

查看:352
本文介绍了在UITableViewCell的子视图上设置遮罩层会覆盖自动布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个子视图的 UITableViewCell 。其中一个子视图是 UILabel ,并且单元格的高度是根据 UILabel 中的文本量动态调整的。这非常有效。

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 text in the UILabel. This works perfectly.

我在单元格中有另一个子视图也有约束。该子视图始终应与单元格具有完全相同的高度。这也很有效。

I have another subview in the cell that has constraints as well. This subview is always supposed to have the exact same height as the cell. This works perfectly as well.

然而,当我尝试在该子视图上设置遮罩层时,我遇到了问题。遮罩层工作正常,但子视图的高度错误,并且与单元格的高度不同。

However, I run into problems when trying to set a mask layer on that subview. The mask layer works correctly, but then the subview's height is wrong and it is not the same height as the cell.

这是我的遮罩层代码:

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 have been doing research and trying to find a way to fix this so I can both set the mask layer and have the subview have it's correct height but I haven't been able to get it to work.

我已经看过这个解决方案现在推荐了几次:

I have seen this solution recommended several times now:

[self setNeedLayout];
[self layoutIfNeeded];
// Customize cell after here

但这对我也不起作用。有没有办法让我知道何时应用了自动布局约束,以便我可以在之后应用遮罩层?

But this doesn't work for me either. Is there a way for me to know when the Auto Layout constraints have been applied so that I can then apply the mask layer afterwards?

掩码层代码非常简单,它使用子视图的边界,并且边界是关闭的,因为它使用了在应用约束之前存在的边界。子视图具有正确的高度。至少我认为我理解正确。

The mask layer code is really simple, it uses the bounds of the subview, and the bounds are off because it's using the bounds that exist before the constraints have been applied and the subview has the correct height. At least I think I'm understanding that correctly.

推荐答案

我终于明白了。我不确定这是否是正确的地方,或者它是否可能导致性能问题,但到目前为止它完美无缺:

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;

}

我必须覆盖 drawRect: 在我的 UITableViewCell 子类中并在那里设置遮罩层。

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

这篇关于在UITableViewCell的子视图上设置遮罩层会覆盖自动布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆