Search code examples
cocoansviewnsscrollview

Custom NSView embedded in NSSscrollView


Is there a way for a custom NSView to know whether it is embedded in a NSScrollView or not?

I am creating a custom NSView for displaying some content.
When my view is placed in a window or another view, its size is fixed and the content is clipped to available size.
When my view is placed in a NSScrollView its size must be adjusted according to content so it can be scrolled if necessary.
I know I can add a member in my view that specifies the NSScrollView that hosts my view and set this member manually in code, but I was wondering if there is another way?


Solution

  • You didn't check the methods of NSView?

    @property(readonly, strong) NSScrollView *enclosingScrollView;

    or

    var enclosingScrollView: NSScrollView? { get }

    The nearest ancestor scroll view that contains the current view.

    If the current view is not embedded inside a scroll view, the value of this property is nil. This property does not contain the current view if the current view is itself a scroll view. It always contains an ancestor scroll view.