Enabling NSScrollView to scroll its contents using Auto Layout in Interface Builder

后端 未结 6 994
时光说笑
时光说笑 2021-01-02 14:17

I have implemented a custom NSView which contains many NSTextFields and other NSViews. I then embedded that custom view in a scroll vi

6条回答
  •  自闭症患者
    2021-01-02 14:44

    To improve on Ken's answer, here is what I did:

    • First, I read this wonderful tutorial about how to set up an NSScrollView in Interface Builder (IB) using auto layout.

    • Then I did the following:

    • Created an NSScrollView instance in IB;

    • Created a new subclass of NSClipView and added the following code:

    class ScCalendarClipView: NSClipView {
        open override var isFlipped: Bool {
            return true
        }
    } 
    

    • Created the custom document view and embedded it as a child of the NSScrollView instance in IB.

    • Added the following constraints in IB in the view I embedded in the NSScrollView instance (let's call it here: DocumentView)

    // Right
    First Item: SuperView.Trailing
    Relation: Less Than or Equal
    Second Item: DocumentView.Trailing
    Constant: 0
    Priority: 510
    Multiplier: 1
    
    First Item: SuperView.Trailing
    Relation: Greater Than or Equal
    Second Item: DocumentView.Trailing
    Constant: 0
    Priority: 490
    Multiplier: 1
    
    // Left
    First Item: DocumentView.Leading
    Relation: Equal
    Second Item: SuperView.Leading
    Constant: 0
    Priority: 1000
    Multiplier: 1
    
    // Bottom (negative constants increase the size of the document view)
    First Item: SuperView.Bottom
    Relation: Less Than or Equal
    Second Item: DocumentView.Bottom
    Constant: -600
    Priority: 510
    Multiplier: 1
    
    // Bottom (constant should be equal to the bottom constraint above)
    First Item: SuperView.Bottom
    Relation: Greater Than or Equal
    Second Item: DocumentView.Bottom
    Constant: -600
    Priority: 490
    Multiplier: 1
    
    // Top
    First Item: DocumentView.Top
    Relation: Equal
    Second Item: SuperView.Top
    Constant: 0
    Priority: 1000
    Multiplier: 1
    

提交回复
热议问题