Re: More Layout Questions


Dave
 



On 21 Sep 2018, at 16:49, Quincey Morris <quinceymorris@...> wrote:

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:

I’m not sure what you mean, my storyboard file has Auto Layout and there are no constraints anywhere in the project. Also, all the old-type layout options are off in the Size Panel in IB.

"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.


Typical! The way I chose to do it, layout sets in own frame rectangle and calls layout on its subviews, however, it seems to work wonderfully, so not sure what is going on! Any comments?


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.

It only calls setNeedsLayout on its subviews, not on itself, is this still not a good idea (it seems to work ok).


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.)

Noted, thanks.

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.

Not sure what you mean by this? There are no constraints define in this Storyboard/NIB.

In the following hierarchy:

WindowTrackerView .view)
SubviewA
SubviewB
SubviewC

WindowTrackerView::layout sets the frame of SubviewA.
SubviewA::layout sets the frame of SubviewB
SubviewB::layout sets the frame of SubviewC



Thanks a lot for the help
All the Best
Dave





























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