Re: More Layout Questions


Quincey Morris
 

On Sep 21, 2018, at 05:36 , Dave <dave@...> wrote:

Using manual layout, does setting the “frame: of a view cause “layout” to be called or do I need to call “setNeedsLayout” specifically?

You seem to be way off base here. “Manual layout” is just the absence of auto-layout. The layout method is part of the auto-layout system, as the documentation says:

"Perform layout in concert with the constraint-based layout system.”


It’s a way of customizing auto-layout, not doing manual layout.

It sounds like you’re trying to auto-layout, but without any constraints. That’s certainly possible, but an auto-layout pass is still an auto-layout pass.

is there a particular way in which the NSView “layout” method is supposed to work or is it up to the developer to choose?

Yes, there’s a particular way. It needs to lay out its subviews (adjust the size and position of its subviews) by doing whatever isn’t being done by constraints. That means setting frames of its subviews. A view should not change *its own* frame in its "layout” method. That invalidates the layout of the superview, which is explicitly prohibited by the documentation you quoted.

At this point there are two options:

1. WindowTrackingView::layout simply calls “setNeedsLayout” for each of its subviews and each subview sets their own frame based on the “frame" rectangle of the superview via their own “layout” method.

No. Do *not* call “setNeedsLayout” from a “layout” method. That will trigger another layout pass, and layout will loop forever. As above, views are prohibited from setting their own frame in their own “layout” method.

2.  WindowTrackingView::layout calculates and sets the frame of each of its subviews based on its own “frame” in this case and then calls “setNeedsDisplay” (unless this is already done if the “frame” changes?).

Do not call “setNeedsDisplay” routinely here. The “layout” method is about layout, and you aren’t concerned with drawing here. (If changing the layout resizes some subviews, the resizing itself will trigger drawing as necessary. That’s the normal consequence of resizing views. You don’t have to do anything extra.)

But, yes, you need to set the frame of each subview that isn’t going to be (before "[super layout]”) or hasn’t been ("after [super layout]”) set by auto-layout constraints.

Join cocoa@apple-dev.groups.io to automatically receive all group messages.