Hiren Prajapati
Hiren Prajapati

Reputation: 727

Should I use autolayout in my new Xcode project?

Auto layout makes things easy but still confuses me a lot when it comes to landscape orientation. I have to keep both landscape as well as portrait screen. My application will be for iPhones only i.e from iPhone 5 to iPhone 6s+.

What size class I need to use? everything seems so confusing. Every constraints conflict with each other. I have no problem in understanding leading, top, bottom, trail space. But what if device is rotated? How do I manage things? Please help!

Upvotes: 1

Views: 163

Answers (2)

rob mayoff
rob mayoff

Reputation: 385500

This site is a useful visual reference for size classes.

If you're making an iPhone-only app, what you basically need to know is that every iPhone uses regular height in portrait, and compact height in landscape. So focus on those two size classes.

Set the storyboard size class (in the bar under the canvas) to wAny hRegular. Then select your view controller and (in the Attributes inspector) set the size to iPhone 4-inch (or other device of your choice) and set the orientation to portrait. (These view controller settings don't interact with the size class but make it easier to see how things will look at runtime.) Now set up your portrait layout constraints.

Then set the size class to wAny hCompact. Change the view controller's orientation to Landscape. Now set up your landscape layout constraints.

Upvotes: 1

Ben Guild
Ben Guild

Reputation: 5116

I don't think you'll need size classes unless you're sure that there's going to be a significant GUI variations between different screen sizes or rotations.

In terms of rotation, the constraints don't change their orientation when the device is rotated. They still stand for what they stood for before.

For size classes, it depends on the app's design. Most likely, if you've setup your constraints correctly, you'll be fine with just a single size class for an entire family of devices.

If you choose to, for example, have significant changes in a tablet-focused variation, you can make those in code depending on the device type or screen size, or use a size class. Only use a size class to have variations in layout between various screen sizes that cannot be handled with constraints alone.

Upvotes: 2

Related Questions