objective-c - UIView subview 中的 setNeedsLayout

标签 objective-c ios cocoa-touch uiview setneedsdisplay

我有一个 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/

相关文章:

ios - 将 Dictionary 转换为 Json 以在 Alamofire 中传递参数

ios - 应该如何开始后台任务和过期处理程序 : be dealt with for an NSUrlConnection that is already in progress?

iphone - 读取静态库中的.pch文件

cocoa - 不使用现有 NSCoding 方法来实现 NSCopying 的任何理由

objective-c - 是否可以在 UIAlertView 中显示图像?

ios - 如何从 Dynamic NSMutableArray 添加坐标到 CLLocationCoordinate2D?

objective-c - iOS/ManagedObjectContext 中的内存管理

ios - 如何解析 facebook 喜欢 json 响应?

ios - “NSApp”在 Mac 的 UIKit 中不可用 : How to embed Mac only framework in iOS app on macOS?

iphone - 我如何知道我的应用程序即将变为非事件状态/进入后台状态?