我有一个带有多个 subview 的 UITableViewCell
。 subview 之一是 UILabel
,单元格的高度根据 UILabel
中的文本量动态调整大小。这完美地工作。
我在单元格中有另一个 subview 也有约束。这个 subview 总是应该与单元格具有完全相同的高度。这也很完美。
但是,尝试在该 subview 上设置 mask 层时遇到了问题。 mask 层正常工作,但是 subview 的高度错误并且与单元格的高度不同。
这是我的蒙版层代码:
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;
我一直在做研究并试图找到一种方法来解决这个问题,这样我就可以设置 mask 层并使 subview 具有正确的高度,但我无法让它工作。
我现在已经多次看到推荐的这个解决方案:
[self setNeedLayout];
[self layoutIfNeeded];
// Customize cell after here
但这对我也不起作用。有没有办法让我知道何时应用了自动布局约束,以便我可以在之后应用 mask 层?
mask 层代码非常简单,它使用 subview 的边界,而边界是关闭的,因为它使用了应用约束之前存在的边界并且 subview 具有正确的高度。至少我认为我理解正确。
最佳答案
我终于明白了。我不确定这是否是放置它的正确位置,或者它是否可能导致性能问题,但到目前为止它运行良好:
- (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
并在那里设置 mask 层。关于ios - 在 UITableViewCell 的 subview 上设置 mask 层会覆盖自动布局约束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29637211/