I am overriding `NSView`’s `layout` method to manually place one of my subviews.

I can set the frame for this subview, and it works until someone calls `layoutSubtreeIfNeeded` on the subview (which sometimes happens automatically during display).

The subview’s `needsLayout` property is `NO` but `layoutSubtreeIfNeeded` will still go ahead and change the frame.

The problem seems to be that `layoutSubtreeIfNeeded` calls `resizeWithOldSuperviewSize:` (using the containing view’s size), but regardless of what’s passed to the view, it’ll set the entire frame and using values from its constraints (falling back to zero if there isn’t any).

That is, if the view has hugging and/or intrinsic content size then the size from the derived constraints will be used, otherwise `NSZeroSize`. Likewise the origin will be `NSZeroPoint` unless constrains are added to affect the position.

So how can I ensure the position I set for a subview’s frame in my `layout` override is not (implicitly) changed later?

Worth mentioning that if I overload `resizeWithOldSuperviewSize:` like this:

        - (void)resizeWithOldSuperviewSize:(NSSize)aSize
        {
                self.frame = (NSRect){ self.frame.origin, aSize };
        }

Then my subview stays put, but the size can be changed. If I simply use an empty body in the `resizeWithOldSuperviewSize:` override, then I have not seen any issues with the frame of my custom laid out subview. But of course that is not an acceptable solution.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to