Enhance your career in iOS development by learning to work with stack views and the secrets to iPhone auto layout. This comprehensive tutorial covers everything from launching Xcode, laying out the UI, to setting content hugging and compression resistance priorities.
Key Insights
- The tutorial starts by teaching you how to launch Xcode and set up a new project with specific details like product name, team, organization name, and language.
- You will then learn how to layout the UI by using elements like an image view, label, and button, and embedding them in a stack view for better alignment and organization of elements.
- The tutorial emphasizes the importance of setting the content mode of the image view to "Aspect Fit," which ensures the proper aspect ratio for the image.
- You will also learn to set the alignment, distribution, and spacing attributes for the stack view to ensure a consistent presentation across different iPhone models.
- Content hugging and compression resistance priorities are explained, and you are shown how to set these attributes for specific elements within the stack view, such as an image.
- The tutorial concludes by demonstrating how to change the "Constant" for constraints like "Trailing Space," showcasing how this impacts the margin around the outer edges of the view on different iPhone models.
Explore the intricacies of iOS app development with a focus on stack views and iPhone auto layout, complete with step-by-step instructions and exercise overviews.
This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.
Topics Covered in This IOS Development Tutorial:
Basic Stacks, Constraints, Content Hugging Priority, Compression Resistance Priority
Exercise Preview
Exercise Overview
In this exercise we’ll explore stack views and reveal some of the secrets to iPhone auto layout.
Getting Started
Launch Xcode.
Go to File > New > Project.
Under iOS and Application double–click on Application.
-
Set the following:
- Product Name: Auto Layout
- Team: None (Or choose your team if you have one)
- Organization Name: Your Name
- Organization Identifier: com. YourName
- Interface: Storyboard
- Language: Swift
- Leave Use Core Data unchecked. Core Data allows users to actually save data on their devices.
- We won’t be learning how to perform tests for this app, so uncheck Include Tests if it isn’t already.
- Click Next.
- Navigate into the Desktop > Class Files > yourname-iOS App Dev 1 Class folder.
- Check Create Git repository on My Mac if it is not already checked. (This can be used to track changes you make over time.)
Click Create to finish specifying the location of the new Xcode project.
Laying Out the UI
- We need some images for our design. In the Project navigator
, select Assets.xcassets.
- Switch to the Desktop and navigate to yourname–iOS App Dev 1 Class > Auto Layout Images.
- Open that folder and select all four images.
- With Xcode still visible, drag the images to the Assets catalog.
- In the Project navigator, click on Main.storyboard.
- At the bottom of the Editor, set the view to iPhone 13 Pro.
- From the Object library
drag out an Image View.
- In the Attributes inspector, set the Image to flowers.
- Center the image on the view using the blue guides.
- In the Attributes inspector under View > Content Mode, select Aspect Fit. This will keep the proper aspect ratio (width to height) for the image.
- From the Object library, pull out a Label and put it above the image.
- Also pull out a Button and put it below the image.
- Change the label text to Flowers and make Alignment centered.
- Change the button text to Edit.
- Select all three objects: the label, image, and button.
- Click on the Embed in button
and choose the Stack View option. The image should magically enlarge! This is because the objects are filling all available space in the stack.
- In the Document Outline, CTRL–drag the Stack View to the View. Choose Leading Space to Safe Area. This tells it how far from the left edge of the phone to place the stack.
- Do the same thing three more times and choose the following:
- Trailing Space to Safe Area (right edge)
- Top Space to Safe Area (top edge)
- Bottom Space to Safe Area (bottom edge)
- While Stack View is still selected, in the Size inspector, next to Align Trailing to:, click Edit and change the Constant to 0.
- Press Esc to get out of that dialog.
-
Do the same thing to the other three constraints, changing the Constant to 0.
We just told Xcode that we want the stack to go to the very edges of any iPhone. It won’t go quite that far since there is some default margin.
- While Stack View is still selected, in the Attributes inspector, under Stack View, add the following:
- Alignment: Fill
- Distribution: Fill
- Spacing: 8
You may notice the button is floating in a large space at the bottom. We can fix that by changing the – wait for it – content hugging and compression resistance priorities.
- Select the flowers image.
- Go into the Size inspector. Scroll down, and under Content Hugging Priority add the following:
- Horizontal: 250
- Vertical: 249
- Right there under Content Compression Resistance Priority add the following:
- Horizontal: 750
- Vertical: 749
Because these vertical values are slightly less than the other objects, the image will be the first one to change size vs. the others. The image will scale while the other objects stay stuck to the outside of the stack view. (The other objects still have default values of 250 and 750.)
- At the bottom of the Editor, try different-sized phones to see how the image scales and the label and button hug the margin.
Remember when we changed the Constant to 0 for Trailing Space and the other constraints? We’re going to change them again to illustrate how that works.
- To see this effect best, click on the iPhone 4s at the bottom of your screen.
- In the Document Outline select the Stack View.
- In the Size inspector, next to Align Trailing to:, click Edit and change the Constant to 25. Watch the effect after each change.
- Do the same thing to the other three constraints, changing the Constant to 25.
- At the bottom of the Editor, set the view back to iPhone 13 Pro. Try different-sized phones to see how the margin around the outside is bigger. The major variation from phone to phone happens to the image due to the content hugging and compression resistance priorities.