gpt4 book ai didi

objective-c - UIView subview 中的 setNeedsLayout

转载 作者:可可西里 更新时间:2023-11-01 05:38:54 25 4
gpt4 key购买 nike

我有一个 UIViewController,在 willRotateToInterfaceOrientation 中,我正在调用 [self view] setNeedsLayout];

这个 View 调用了它的 subview (UIScrollView)layoutSubviews 方法,但是 UIScrollView 没有调用 layoutSubviews 因为它的 subview 。这是应该的样子吗?我想如果你在一个 View 上调用 setNeedsLayout,它会在每个 subview 和它们的 subview 上调用 layoutSubviews 以及...

我是否必须手动覆盖 UIScrollView 中的 layoutsubview 并为每个 subview 调用 setNeedsDisplay

谢谢。

最佳答案

我前段时间在我的应用程序中观察到同样的事情,似乎 setNeedsLayout 只影响 View 的 subview 而不会向下遍历。如果您使用自定义 View ,您可以轻松地将 View 子类化并在 setNeedsLayout 中迭代 subview ,并对它们执行显式 setNeedsLayout

如果您希望它成为默认行为,您可以覆盖类别中的方法,但我真的不会这样做,因为它会影响所有 View 并可能引入性能和其他意外问题。

你可以这样做

@interface UIView (Layout)

- (void)setNeedsLayoutRecursively;

@end

@implementation UIView (Layout)

- (void)setNeedsLayoutRecursively {
for (UIView *view in self.subviews) {
[view setNeedsLayoutRecursively];
}
[self setNeedsLayout];
}

@end

在浏览器中输入,未经测试

但是,请注意,这不是好的做法!您应该只更新需要更新的内容。这可能会导致严重的性能损失。谨慎使用!

关于objective-c - UIView subview 中的 setNeedsLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7695903/

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