This article explains how to create a NSScrollView using AutoLayout in Interface Builder (MacOS X, Swift 4.2)
Not so long time ago I’ve started writing the application for MacOS (I have IOS development background) and faced the problem that most of the answers on my questions were:
- outdated (answered more than 3 years ago, and consequently, some features were already deprecated)
- (some answers on Objective C were a little bit painful for my “
” eye) swifty
- answers were for iOS, not for OS X
and sometimes finding of a needed answer is harder than it should be.
So I decided to start writing step-by-step swift tutorials (simple tutorials in format question-answer).
Content doesn’t fit application window and being cut by view controller.
To fix this problem you need to add constraints to your views to reach a dynamic width and height of the content.
Okay, let’s start!
(Step 1 – on an image below) First of all drag NSScrollView to your ViewController. Then at the left panel in “Attributes Inspector” select (2)
Border Type —
none and (3) uncheck
After, add top/bottom/leading/trailing constraints related to superview. I set a top constraint to 20 because my NSWindow
Full Size Content View =
Then add your content view(s) (in my case those are 2 Labels wrapped by NSStackView).
Next let’s add the top, leading, trailing constraints and add two bottom constraints to your contentView. I set all constants to 24.
Then (7) change first bottom offset
Less Than or Equal (≤) and set
490, (8) for the second bottom offset
Greater Than or Equal (≥),
(9) In addition to that, I added a minimum contentView width 500 (
width constant =
Greater Than or Equal).
For documentView of scrollView add the top, leading, two trailing and two bottom constraints. (10) Set first trailing offset
Less Than or Equal and set
510, for second
Greater Than or Equal,
490. (11) And do the same for bottom constraints.
At last, you should change the
isFliped property to a true value for
NSScrollView using AutoLayout result
Consequently, we have a simple application with dynamic content size.
I hope this article about NSScrollView using AutoLayout was helpful to you.
You can find the source code here
You may check my apps.
If you have any questions or spot any errors please let me know.
Written with ❤️ by Rita