Automatically grow document view of NSScrollView using auto layout?

前端 未结 1 1824
你的背包
你的背包 2021-01-31 20:50

Is there a simple way to get an NSScrollView to adapt to its document view changing size when using auto layout?

I have tried to call both setNeedsUpd

相关标签:
1条回答
  • 2021-01-31 21:23

    The problem of course is that when adding the document view, Cocoa will automatically create some hard constraints in the view that the document view is inserted into, i.e., the clip view.

    So the answer to my own question is simple, just use:

    // Assume self.docView is an IBOutlet populated with
    // an NSView subclass
    self.docView.translatesAutoresizingMaskIntoConstraints = NO;
    

    before you add the document view to the scroll view:

    self.scrollView.documentView = self.docView;
    

    Now, since there are no auto-generated constraints on the layout of the document view in the clip view, you will need to add them explicitly. Otherwise, the doc view's contents will just be rendered at their intrinsic size in the upper left corner of the scroll view.

    0 讨论(0)
提交回复
热议问题