Unit 18: Adaptive User Interfaces Flashcards
Asset catalog
the best place to store the images for the app; located in .xcassets
Auto Layout
used to build adaptive interfaces
Constraint
a rule in Auto Layout that defines how views should be laid out or sized
Stack view
a rule in Auto Layout that defines how views should be laid out or sized
18.1 - SimpleCentre
You’ll start by building an app to experiment with adaptive user interfaces and learn the basics of Auto Layout.
In Xcode, create a new project.
Use the iOS template for a Single View Application.
Name the product “SimpleCenter.”
Save the project to the desktop.
Select Main.storyboard in the project navigator.
At the bottom of the window you’ll see a button bar:
It indicates you’re currently viewing the canvas at the size of an iPhone 6s.
Use the View as: button to display a set of options for the device size and orientation you to see on the canvas.
Click the button and choose the smallest device (iPhone 4s).
`
Not Adapting
First, you’ll experiment with a layout that doesn’t adapt.
Open the library pane and search for “label.”
Drag a label to the center of the canvas. You’ll see blue dotted lines when you’ve successfully centered the label horizontally and vertically.
Double-click the label and edit its contents to “Hello!”
Notice that the label “Hello!” is still centered top to bottom and left to right.
Now experiment by clicking different device sizes at the bottom of the screen. Does the “Hello!” label stay centered on the screen as you change screen sizes?
Try changing the orientation from portrait (tall) to landscape (wide) mode. Does the “Hello!” label stay centered on the screen in landscape mode?
Finally, go back to the smallest iPhone device size and to portrait mode.
On this screen, the “Hello!” label once again appears in the center vertically and horizontally.
What’s going on?
The size and location of every view in Interface Builder is defined
as a rectangle, known as its frame. This is similar to plotting points
on a coordinate system, which you might remember from math class.
When you add a view, like the “Hello!” label, to the canvas, the view’s location is a certain distance from the top and a certain distance from the left. Those distances remain the same for every screen size and orientation. So the label will appear centered for one layout but will be way off-center for the others.
Making Views Adapt with Auto Layout
A favorite tool that iOS developers use for building adaptive user interfaces is Auto Layout. In your earlier experiment, you saw that the position of the text label stayed the same regardless of screen size or orientation. With
Auto Layout, you set up rules, called constraints, that define how views will be laid out.
Note: As you first learn about Auto Layout, you might find it’s easier to think of the word “rule” whenever you see the word “constraint.”
Auto Layout is very powerful and can be used to create complex layouts. There are many variations
“in how you set up constraints.
In this lesson, you’ll focus on using just a few common constraints that will help you achieve consistent results.
Adding constraints
Check out the bottom of the Interface Builder editor. You’ll see four buttons that will help you build adaptive user interfaces.
The second button from the left , the one that looks like a bar chart,
is the Align button.
A popover appears with various alignment constraints. Some of the constraints aren’t enabled; you’ll only see those when you select more
than one view.
The constraints you want to set right now are Horizontally in Container
and Vertically in Container:
Select those two checkboxes.
Make sure the numbers to the right are set to 0.
Click the Add 2 Constraints button.
Note: Make sure that you check the checkboxes and click the Add 2 Constraints button.
When you run your app, the constraints you added will make sure the view
is centered vertically and horizontally on all screens and orientations.
Fixing Issues
Once you’ve added the constraints, you should see two blue lines,
one vertical and one horizontal. These lines are the constraints that
you just added.
If you don’t see any lines, you may have missed a step in the last section.
For example, it’s easy to forget to click the Add 2 Constraints button after checking the checkboxes. Go back and try again.
If you see orange lines, don’t panic. This next section will help you resolve the issue.
When you’re working in Interface Builder, sometimes the constraints you’ve set up don’t agree with the current frame of a view.
To demonstrate this, drag the “Hello!” label down and a little bit to the left. You might need to click somewhere else on the canvas before selecting
the label; otherwise you could end up selecting a constraint (one of the
blue lines) instead of the label.
Fixing Issues [cont’d]
Leave the label selected after you move it. The canvas should look something like the image to the left.
Now your lines have turned orange, indicating that the current frame
of the label doesn’t agree with the constraints you set. The outlined orange rectangle shows you where the constraints say the label should go, and the numbers on the orange vertical and horizontal lines indicate how far the label needs to move.
Whenever the frame of a view on the canvas is different from where the constraints will put the view when you run the app, you’ll have a problem. You can fix it in one of two ways:
Update the frame to match the constraints.
Update the constraints to match the frame.
In fact, Interface Builder can fix the issue for you. To make it happen, start
by locating the Resolve Auto Layout Issues button, the right-most button
in the Auto Layout button group .
Clicking the button brings up a menu of choices.
In this case, you want the label to be centered, so you’ll update the frame
of the label view.
Select the label, if it isn’t already.
Click the Resolve Auto Layout Issues button.
Select Update Frames from Selected Views.
Notice that the label moved back to the center of the canvas and the lines turned blue again. The frame of the label agrees with the constraints.
Testing
Now that you’ve added constraints to keep the label centered horizontally and vertically, test that it works on other screens.
Like you did before, choose different devices to change the size of the canvas. Also change the orientation from portrait to landscape.
The label view should stay centered for every screen size and orientation. The layout is automatically adapting based on the constraints you’ve applied.
Congratulations! You’ve built an adaptive user interface.
18.2 ElementQuiz
In the previous part of this lesson, you created a simple app to learn how
to center a view horizontally and vertically on different screens. In this part, you’ll build an app to quiz users on the elements of the periodic table. The app will work a bit like flash cards. The user sees the element symbol and atomic weight, then taps a button to reveal the name of the element.
You’ve already learned that constraints in Auto Layout provide a powerful way to build an adaptive user interface. But there’s a degree of complexity in that power. In many apps, the main user interface is similar to a stack or list of views, and the stack works either from top to bottom or from left to right. You can use constraints in Auto Layout to define these kinds of interfaces, but iOS provides a more convenient approach. It’s called a stack view.
In your quiz app, you’ll use a UIStackView to arrange the views from top to bottom. You’ll then use constraints to center the stack view on the canvas. Along the way, you’ll also use constraints for the width and height of an image view…
ElementQuiz [cont’d]
Select Main.storyboard in the project navigator.
Click the View as: button and select the smallest device (iPhone 4s).
The user interface of this app consists of four views, from top to bottom:
An image view to display an image of the chemical element
A label to display either a question mark or the name of the element
A button to show the answer
A button to go to the next element
Drag these items from the library onto the canvas. Remember that you can use the filter bar at the bottom of the library to narrow your search. Arrange the views from top to bottom.
Image View Size
When you use Auto Layout with an image view, the view, by default, will adjust its size to fit the size of the image. But you often want to make sure the image view stays a fixed size. That’s the case with the ElementQuiz app.
Change the size of the image view:
Select the image view.
In the Size inspector, set the width to 140 and the height to 140.
Note that changing the size in the Size inspector doesn’t set constraints; it only adjusts the size in Interface Builder. Similar to what you did in the first part of this lesson, you need to set constraints to have views adapt using Auto Layout and stack views.
To set width and height constraints on the image view:
Select the image view.
Click the Pin button at the bottom of the Interface Builder editor.
A popover appears with various constraints. Use each constraint to set,
or pin, some aspect of the layout of the view to a desired value.
The constraints you’re interested in are Width and Height. To set those constraints:
Select those two checkboxes.
Make sure the numbers to the right are set to 140.
Click the Add 2 Constraints button.
Remember: It can be easy to forget to click the Add 2 Constraints button.
The canvas should look like the image to the left, with red lines around the
image view. The red lines mean you haven’t defined enough constraints for Auto Layout to figure out the position and the size of the image view. In this case, you’ve defined the size, but you haven’t defined the position of the view. You’ll do that a little later in this lesson.
Configuring Label and Buttons
Before arranging all these views in a stack view, you’ll need to configure
the label and the buttons.
Configure the label:
Select the label.
In the Attributes inspector, click the Font button to display the Font
popup menu.
Change the Style to Bold and the Size to 24, then click Done (see left).
Change the string in the label to “Answer Label.”
Don’t worry if the label isn’t centered or if the text is a bit cut off. You’ll fix those problems in the next section.
Even though the contents of this label will be changed at runtime, setting
a string value besides “Label“ makes the purpose of the label clear.
Configure the buttons:
Set the title of the button below the label to “Show Answer.”
Set the title of the bottom button to “Next Element.”
For now, the text of the buttons may be truncated. You’ll also address that issue in the next section.
Adding a Stack View
At the moment, the views you’ve added aren’t very tidy. That’s fine, as long as they’re arranged top to bottom. In this section, you’re going to put all of those views into an instance of UIStackView. Stack views keep views arranged either vertically or horizontally.
To put all four views in a stack view:
Select the four views. You can Shift-click to select multiple items or drag over all four items on the canvas. When you’re finished, the canvas should look similar to this, with white selection handles visible at the edges of the selected views.
With the views selected, click the Stack button at the bottom of the Interface Builder editor .
The canvas should look similar to this (see left).
Note that the views in the stack will appear either centered, left-aligned,
or right-aligned, depending on how the views were arranged before you clicked the Stack button.
Centering the Stack View
To define the position of the stack view, you’ll add two constraints. You’ll use the technique you learned in the last part of the lesson to center the stack view vertically and horizontally. Since your four views are now contained in the stack view, they’ll all be centered as well.
It can be tricky to select a stack view. Choose one of these techniques:
Drag over the stack view from outside the stack view.
Select Stack View from the outline view (see image left).
Notice the hierarchy in the outline view. You can see that the stack view contains the image view, the label, and the two buttons.
To add constraints to the stack view, go through the same steps you used
to center the text label in Part 1 of this lesson:
Select the stack view using one of the techniques above.
Click the Align button at the bottom of the Interface Builder editor.
Add constraints for Horizontally in Container and Vertically in Container (see image left).
More Orange Lines
After you add these constraints, you’ll probably see orange lines on the canvas. You just set constraints to center the stack view vertically and horizontally, but you can see that the stack view still isn’t centered. As
you learned in Part 1, the orange lines indicate that the frame of the view
doesn’t agree with the constraints. That’s clearly what’s happening here.
You’ll fix the issue the same way you did in Part 1:
Make sure the stack view is selected.
Click the Resolve Auto Layout Issues button at the bottom of the Interface Builder editor .
Choose Update Frames from Selected Views.
Note: If the Update Frames menu item is not enabled, click inside the canvas to deselect the stack view, then select it again.
The stack view should now be centered on the canvas, and blue lines showing the constraints to center horizontally and vertically should
be visible.
Configuring the Stack View
The last step in building the user interface is to configure the stack view.
You can change various properties of a stack view to get the result you want.
In this case, all the views should be centered horizontally within the stack view. But the views also need some vertical room between them, so you’ll need to increase the spacing a bit.
The two stack view settings you’ll use are:
Alignment: How the views are arranged within the stack.
Spacing: How much space is between each view in the stack.
To configure the stack view:
Select the stack view.
In the Attributes inspector set Alignment to Center and Spacing to 12.
The views should now appear centered horizontally in the stack view with
a nice amount of vertical space between them.