tags:

views:

973

answers:

3

Hi all. Can anyone give a definitive explanation on the relationship between UIView's setNeedsLayout, layoutIfNeeded and layoutSubviews methods? And an example implementation where all three would be used. Thanks.

What gets me confused is that if I send my custom view a setNeedsLayout message the very next thing it invokes after this method is layoutSubviews, skipping right over layoutIfNeeded. From the docs I would expect the flow to be setNeedsLayout > causes layoutIfNeeded to be called > causes layoutSubviews to be called.

+1  A: 

Apple's documentation is pretty good.

Alex Reynolds
And also far from clear, which is the point of this question.
leolobato
+2  A: 

I'm still trying to figure this out myself, so take this with some skepticism and forgive me if it contains errors.

setNeedsLayout is an easy one: it just sets a flag somewhere in the UIView that marks it as needing layout. That will force layoutSubviews to be called on the view before the next redraw happens. Note that in many cases you don't need to call this explicitly, because of the autoresizesSubviews property. If that's set (which it is by default) then any change to a view's frame will cause the view to lay out its subviews.

layoutSubviews is the method in which you do all the interesting stuff. It's the equivalent of drawRect for layout, if you will. A trivial example might be:

-(void)layoutSubviews {
    // Child's frame is always equal to ours inset by 8px
    self.subview1.frame = CGRectInset(self.frame, 8.0, 8.0);
    // It seems likely that this is incorrect:
    // [self.subview1 layoutSubviews];
    // ... and this is correct:
    [self.subview1 setNeedsLayout];
    // but I don't claim to know definitively.
}

AFAIK layoutIfNeeded isn't generally meant to be overridden in your subclass. It's a method that you're meant to call when you want a view to be laid out right now. Apple's implementation might look something like this:

-(void)layoutIfNeeded {
    if (self._needsLayout) {
        UIView *sv = self.superview;
        if (sv._needsLayout) {
            [sv layoutIfNeeded];
        } else {
            [self layoutSubviews];
        }
    }
}

You would call layoutIfNeeded on a view to force it (and its superviews as necessary) to be laid out immediately.

n8gray
Thank you - glad someone finally answered this. In the meantime I've also had to delve into setNeedsDisplay - which causes drawRect to be called - and contentMode. Only contentMode = UIViewContentModeRedraw will cause setNeedsDisplay to be called when a view's bounds changes. The other contentMode choices only cause the view to be scaled, shifted by some amount, or aligned to an edge. My custom UITableViewCell didn't want to redraw on orientation changes, it would only scale, until I set contentMode to UIViewContentModeRedraw.
Tarfa
I think perhaps the [self.subview1 layoutSubviews] in your code should be replaced with [self.subview1 setNeedsLayout]. Since layoutSubviews is meant to be overridden but is not meant to be called from or to another view. Either the framework determines when to call it (by grouping together multiple layout requests into one call for efficiency) or you indirectly do by calling layoutIfNeeded somewhere in the view hierarchy.
Tarfa
Correction to my above comment: "...Since layoutSubviews is meant to be overridden or called from self but is not meant to be called from or to another view..."
Tarfa
I'm really not sure about `[subview1 layoutSubviews]`. It could very well be that, like `drawRect`, you're not supposed to call it directly. But I'm not sure if calling `setNeedsLayout` during the layout phase will cause the view to be laid out during the same layout phase or if it's delayed until the next one.It would be nice to see an answer from somebody who *really* understands how this all works...
n8gray
A: 

@leolobato, can you please explain the same clearly.

santosh kumar