More Layout Questions


Dave
 

Hi,

I have a couple of questions related to manual layout on the Mac.

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


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


From the docs:

Override this method if your custom view needs to perform custom layout not expressible using the constraint-based layout system. In this case you are responsible for setting needsLayout to YES when something that impacts your custom layout changes.

You may not invalidate any constraints as part of your layout phase, nor invalidate the layout of your superview or views outside of your view hierarchy. You also may not invoke a drawing pass as part of layout.

You must call [super layout] as part of your implementation.

Should the “layout” method change the value of their “frame” property or should this be done by the superview?

I have a View Hierarchy and when the window resizes, I change the sizes and positions of the views within each subview. It seem to me that there are two ways of doing it given the following Hierarchy:

WindowTrackingView (.view)
LeftAreaView
ContainerViewA

RightAreaView
ContainerViewB

So, when the window resized, WindowTrackingView::layout gets called with its “frame” property set to the new size. 

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.

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

Is any of these methods preferred or is it up to the developer to choose based on the job in hand?


Any help on this greatly appreciated, I’ve written a test app and have almost got my head around manual layout, I just need to understand if this last bit.

Thanks in advance.

All the Best
Dave















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