Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does NSView have anything analogous to UIView's setNeedsLayout/layoutSubviews methods?

Do I put such things into the display method? Or is there something analogous?

like image 263
William Jockusch Avatar asked Jan 13 '11 03:01

William Jockusch


People also ask

When should I use layoutSubviews?

layoutSubviews() The system calls this method whenever it needs to recalculate the frames of views, so you should override it when you want to set frames and specify positioning and sizing. However, you should never call this explicitly when your view hierarchy requires a layout refresh.

How many times is layoutSubviews called?

layoutSubviews is called when the view is created, but never again. My subviews are correctly laid out. If I toggle the in-call status bar off, the subview's layoutSubviews is not called at all, even though the main view does animate its resize.


2 Answers

As of OSX 10.7:

- (void)layout is equivalent to layoutSubviews

There is now an identical setNeedsLayout.

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 calling setNeedsLayout: 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.

like image 129
Cocoanetics Avatar answered Oct 06 '22 11:10

Cocoanetics


Analogous to layoutSubviews is the resizeSubviewsWithOldSize: method of NSView. I guess, analogous to setNeedsLayout would be calling resizeSubviewsWithOldSize:[self frame].size directly.

like image 36
Dominik Seibold Avatar answered Oct 06 '22 11:10

Dominik Seibold