UIView 子视图中的 setNeedsLayout

发布于 2024-12-08 22:20:31 字数 473 浏览 0 评论 0 原文

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

这个视图调用是它的子视图(一个 UIScrollView ) layoutSubviews 方法,但 UIScrollView 不会调用 layoutSubviews这是子视图。事情应该是这样的吗?我想如果你在视图上调用 setNeedsLayout ,它会在每个 subivew 及其子视图上调用 layoutSubviews ......

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

谢谢。

I have a UIViewController and in willRotateToInterfaceOrientation, i am calling [self view] setNeedsLayout];

This view call's it's subview's (a UIScrollView) layoutSubviews method, but the UIScrollView doesn't call layoutSubviews for it's subviews. Is this how it's suppose to be? I thought if you call setNeedsLayout on a view, it will call layoutSubviews on each and every subivew and their subviews and on...

Do I have to manually, override layoutsubview in UIScrollView and call setNeedsDisplay for each of the subviews?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-12-15 22:20:31

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

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

您可以执行类似的操作

@interface UIView (Layout)
  
- (void)setNeedsLayoutRecursively;

@end

@implementation UIView (Layout)

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

@end

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

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

I observed the same thing in my app some time ago and it seems that setNeedsLayout only affects the view's subviews and does not traverse down. If you use custom views you can easily subclass the views and iterate over the subviews in setNeedsLayout and perform an explicit setNeedsLayout on them.

If you want it to be the default behavior you could override the method in a category, but I really would not do this since it affects all views and may introduce performance and other unexpected issues.

You could do something like this

@interface UIView (Layout)
  
- (void)setNeedsLayoutRecursively;

@end

@implementation UIView (Layout)

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

@end

typed in browser, not tested

However, be aware that this is not good practice! You should only update what needs updating. This could result in a severe performance hit. Use with caution!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文