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

后端 未结 6 995
时光说笑
时光说笑 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:40

    The NSScrollView needs to have an X, Y, width and height constraint so that it keeps it's frame the same even if you have views in NSClipView that have bigger width/height that the ScrollView.

    I have seen that if you have views with bigger dimensions and you don't have these constraints, the NSScrollView's frame is automatically set to be the same as the NSClipView, which would be a bigger frame and it may exceed the size of the Window's Frame.

    That is why sometimes it might appear that the NSScrollView doesn't scroll, but it is actually big enough that it contains all the views, exceeding the Window's frame, and it does not need to scroll.

    I made it work by doing this :

    1. Set the following constraints for NSScrollView

      • Leading Space to Container
      • Top Space to Container
      • Equal Widths
      • Equal Heights

    As seen below

    1

    1. Do the same for every View in NSClipView

      The Views will also keep their sizes after you change the frame size of the NSScrollView, if you want to do that.

    In my case it worked like charm! I have an ImageView inside the NSClipView and it scrolls after you resize the window as well. Hope it works!

提交回复
热议问题