OS X Stack Views with NSStackView
In this tutorial you will learn how to use stack views to design your OS X app’s layout by leveraging the power of NSStackView. By Marin Todorov.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
OS X Stack Views with NSStackView
25 mins
- What is a Stack View?
- When to Use a Stack View in Your OS X App?
- The Tutorial Project
- Getting Started
- Your First Stack View
- Nesting Stack Views
- Customizing the Stack
- Full Window Layout
- Full Table Cell Layout
- Using Constraints on Arranged Views
- Working with the Arranged Views
- Stack View Animations
- Where to Go From Here
Customizing the Stack
So far, you’ve created a number of stack views and hopefully you’re starting to feel like a pro. :] You have the default stack view configuration, however, and in this section you’ll see how customizing the default behavior can provide even more flexibility at almost no cost.
The stack views you created so far tutorial grew in size along with their content. In a way, you’ve only been “wrapping” views together for the sake of aligning them and nesting stacks.
Stacks though can behave a bit differently if you fix their size and let them arrange their sub-views within that given space.
Full Window Layout
So far, you’ve got two “top” layout elements in your app’s UI. The former is the table on the left, and the latter is the stack view that wraps all the details about a single book plus the purchase button.
No matter how you arrange those elements you ultimately want them to spread nicely within the app’s window.
In the document outline select the table view and the top stack view you have so far:
Next — no surprise here — click the Stack button at the bottom of the Interface Builder pane to stack the two selected views together, effectively bundling them into a new horizontal stack.
Now click on the Pin button, which is located close to the Stack button, and enter four zeroes in the four boxes at the top of the popup. Make sure the four red lines light up while you enter the numbers in.
Finally, click Add 4 Constraints to pin your top stack view to the window, effectively making it a “full window” view.
Just to make sure all views display correctly, click Resolve Auto Layout, located to the right of the Pin button, and choose Update Frames under All Views in View Controller. This will apply all current constraints and your layout will look like this:
Since the table view has a constraint that sets its width to 180
points, the stack view respects that and lets the other arranged view fill all of the remaining space. Run the project and try resizing the window.
The table view fills up the window vertically but it always keeps its width because the width is pinned with a constraint.
Shift your attention to the book details. As you resize the window, the cover image and the texts always stay centered. Since the stack that contains them grows or shrinks to fill up the space not taken by the table view, its sub-views always stayed centered.
This is all you needed to do to make your layout fill up the window. That was easy, n’est–ce pas?
Now look into another area of the layout where some of that same magic you just did could come handy.
Full Table Cell Layout
The table view doesn’t look all that nice at the moment, kind of like someone just threw in an image view and a label without any concern for alignment. Annoyingly, the text gets cut out at run time:
Time for you to add some stack view goodness to that table view.
In document outline, select the cell image and label. To do that, you’ll need to drill down through the view hierarchy as shown:
Aaaand…drumroll…you guessed it: click the Stack button in Interface Builder. This will bundle the image and label into a vertical stack view.
With the stack still selected, set the alignment to Center X. While you still have the stack selected, click on the Pin button at the bottom of Interface Builder and pin the stack to its parent view in all directions.
Since the nearest neighbor view of the stack view is the cell view, simply pin the stack to the cell itself. Click Add 4 Constraints to finish up and close the popup.
Select the stack view and click the Resolve Auto Layout issues button (to the right of Pin) and select Update Frames. The stack view takes up the whole table cell space and it now looks like this:
Run the app and note how the stack view makes your whole table layout work like a charm.
Using Constraints on Arranged Views
Stack views give you the power of Auto Layout without the hassle of creating all the constraints yourself, but you can still design constraints manually if you want. And sometimes, you do want to do that; sometimes default settings just don’t work. :]
There’s a few small issues with your layout that would benefit from a few constraints.
Select the Buy now from raywenderlich.com button and click the Pin button at the bottom of Interface Builder. From the popup menu, click the checkbox next to width and enter 250 in the box.
Click Add 1 Constraint to make it so. You’re setting the button width to 250 points, giving it nice padding on the sides so it’s easy on the eyes.
Adding custom constraints is that easy!
How about you align the window contents to the top of the window? Start by selecting your current top stack view.
In Attributes Inspector, set alignment to Top. This should move the book details views up the window, like so:
Now you’re going to add a bit of spacing on the top so that the cover image is not “stuck” to the window title bar. Select the stack containing the book details (second down the hierarchy):
Click the Pin button and add a Top constraint with the value of 20 points:
Click Add 1 Constraint, and you’ll see that the book details stack view now has 20 points as a top margin.
As you see, you can leave stacks to apply the default behavior and just add a constraint here and there to customize and perfect the layout!
Working with the Arranged Views
Now that you know how to stack up your layouts, the next level is to play around with the stack contents.
The stack view itself is just a container and displays nothing on screen. It merely arranges the layout of a bunch of views, and you can access those via the arrangedSubviews
property. For each arranged view, you can show and hide, animate, or remove it from the stack at will.