gpt4 book ai didi

ios - 在 UITableViewCell 的 subview 上设置 mask 层会覆盖自动布局约束

转载 作者:可可西里 更新时间:2023-11-01 06:18:09 24 4
gpt4 key购买 nike

我有一个包含多个 subview 的 UITableViewCell。其中一个 subview 是 UILabel,单元格的高度根据 UILabel 中的文本量动态调整大小。这非常有效。

我在单元格中还有另一个 subview 也有约束。该 subview 始终应该与单元格具有完全相同的高度。这也非常有效。

但是,我在尝试在该 subview 上设置 mask 层时遇到了问题。 mask 层工作正常,但 subview 的高度错误,与单元格的高度不同。

这是我的 mask 层代码:

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;

}

我不得不在我的 UITableViewCell 子类中覆盖 drawRect: 并在那里设置 mask 层。

关于ios - 在 UITableViewCell 的 subview 上设置 mask 层会覆盖自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29637211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com