NSScrollView won't scroll

Hi,


I'm trying to do a really simple thing in theory: load some content programmatically into a scroll view, and then be able to scroll through said content. A screenshot of my storyboard set up can be found here: https://drive.google.com/file/d/1-0P_5jx85H-RpVJM7JFxYCACn8h3mQpm/view?usp=sharing


I dragged an NSScrollView into the main view, and renamed it Scroll View Container. It automatically created those subclasses. The one labeled Scroll View is an NSView. In my Swift code, I add some WKWebView objects and text labels to the Scroll View (NSView) programmatically by doing self.scrollView.addSubview(<new element>). However when I run it and the web views are added, the view still isn't scrollable (see image: https://drive.google.com/file/d/1-bJDxP7UM-r-4rJ6f1MXfMOzd71JnRj_/view?usp=sharing).


I've scoured the internet and tried every possible option I could find: changing the frame height of the NSView programmatically, changing the frame height of the NSScrollView's documentView and contentView, adding a height constraint and updating that in the code, messing with the constraints in the storyboard builder, etc.


I think it might have something to do with the fact that in the first image above you can see the NSView has a handle at the top to change its height by dragging up (which then makes some scroll bars appear in the storyboard view) but no handle at the bottom to change the height by dragging down. And when I add my web views and stuff programmatically, I use their bottom and top constraints to add each element below the previous one. I saw some posts online about an isFlipped property, but I don't know how to change it and am not sure whether that's what I need anyway. Please help!! Thank you!

Accepted Reply

Actually, it doing exactly what you asked for.


The view you call "Scroll View" (but please don't do that, because people are going to think you mean the NSScrollView) is the view that has the stuff that you want to scroll over (the "document view", in the official terminology). Therefore, the document view has to be the size of the stuff, not the size of the NSScrollView or the NSClipView. You've added constraints to make it the same size as the NSScrollView, so it will never have any reason to scroll.


What you should do instead is determine the height and width of the stuff you want to show, and then programmatically set the bounds or frame of the document view.

Replies

First, sorry if it is stupid question


How do you command the scroll ?

On mouse, (using one finger move)

or on a track pad ? (using 2 fingers move)


If that's not the problem, could you show the code ?

Actually, it doing exactly what you asked for.


The view you call "Scroll View" (but please don't do that, because people are going to think you mean the NSScrollView) is the view that has the stuff that you want to scroll over (the "document view", in the official terminology). Therefore, the document view has to be the size of the stuff, not the size of the NSScrollView or the NSClipView. You've added constraints to make it the same size as the NSScrollView, so it will never have any reason to scroll.


What you should do instead is determine the height and width of the stuff you want to show, and then programmatically set the bounds or frame of the document view.

Wow. Thank you! All I had to do was remove that bottom constraint in the storyboard view. 😊 And thanks for the terminology correction!