4

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.

atxe
  • 5,029
  • 2
  • 36
  • 50
0xSina
  • 20,973
  • 34
  • 136
  • 253

1 Answers1

8

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!

  • Thanks..I was hoping I didn't have to do that, wanted to avoid that. Don't really want to subview alot of UIViews as I have a deep view hierarchy. Anyways, thanks alot for your help. – 0xSina Oct 08 '11 at 23:52
  • excellent little opt-in codelet that solved my problem perfectly. thank you! – jd. Apr 17 '12 at 23:50
  • But child views shouldn't have to worry about the parent's state. Only if the bounds of the child view changed does the child need to be laid out again. This can happen if the parent's layoutSubviews (or autoresize masks) change the child's size. In that situation, layoutSubviews propagates down the view hierarchy. – Kartick Vaddadi Apr 08 '16 at 07:48
  • @VaddadiKartick yes, use with caution. Amended the answer. –  Aug 03 '20 at 09:40