40

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

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • possible duplicate of [cocoa -- What is the proper way to tell an NSWindow to redisplay its contents?](http://stackoverflow.com/questions/4676412/cocoa-what-is-the-proper-way-to-tell-an-nswindow-to-redisplay-its-contents) – Sixten Otto Jan 13 '11 at 04:30
  • 4
    I think it's a different question. One is asking how to tell the window to redisplay everything from scratch. The other is asking about layout only. – William Jockusch Jan 13 '11 at 17:16
  • Indeed: this is a different question. – codeperson Feb 02 '14 at 19:57
  • 1
    This question has nothing to do with windows. It's related to NSView. – jeremywhuff Jul 28 '17 at 19:13

4 Answers4

17

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.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Cocoanetics
  • 8,171
  • 2
  • 30
  • 57
13

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

Dominik Seibold
  • 2,439
  • 1
  • 23
  • 29
2

resizeSubviewsWithOldSize: seems like it does nothing. Never gets called at all for me. Maybe it's because I use autolayout and the documentation says it's related to autoresizing.

NSView's layout appears to be the same as UIView's layoutSubviews. They're both overridable if you want to do some special work to replace or in addition to autoresizing or autolayout.

Calling this on UIView

[view setNeedsLayout];

seems to be the same as this on NSView

view.needsLayout = YES;

Which begs the question, why is "setNeedsLayout" even a function name? Like, really, you decided to remove the parameter from a setter function, and keep the "set" in the title? Why not "scheduleLayout"? This would obviate the need for Stack Overflow questions like this.

Pang
  • 9,564
  • 146
  • 81
  • 122
jeremywhuff
  • 2,911
  • 3
  • 29
  • 33
  • Thanks for posting your findings. – mattsven Jul 28 '17 at 19:51
  • The reason is because there's no such thing as a write-only property and they don't want you querying this status. Why this distinction exists across platforms I can't say, pick one Apple! – Procrastin8 Dec 11 '20 at 15:56
1

Sometimes, resizeSubviewsWithOldSize doesn't get called. Then, try overriding resizeWithOldSuperviewSize.

eonil
  • 83,476
  • 81
  • 317
  • 516