22
Auto Layout (Xcode 5)

Auto Layout on Xcode 5

Embed Size (px)

DESCRIPTION

Apple did a huge improvement on Autolayout in Xcode 5 such as the shortcuts on IB, controls, and ease of use. I think we should give auto layout a second chance since it will be a big help for us in the future, especially when Apple releases new devices with different screen sizes. Auto layout can help reduce large amounts of code.

Citation preview

Page 1: Auto Layout on Xcode 5

Auto Layout(Xcode 5)

Page 2: Auto Layout on Xcode 5

Seeing this view with all the lines will make you freak out.

Xcode 4

Page 3: Auto Layout on Xcode 5

There is a rumor that iPhone 6 will have a new size 4.7 and 5.5 inch.

Imagine how you will handle the different screen sizes

Page 4: Auto Layout on Xcode 5

Autoresizing Mask

Autoresizing mask can help us with our views but it’s too limited.

Page 5: Auto Layout on Xcode 5

Auto Layout To The Rescue!

Page 6: Auto Layout on Xcode 5

Auto Layout is a system that lets you lay out your app’s user interface by creating a mathematical description of the relationships between the elements.

You define these relationships in terms of constraints either on individual elements, or between sets of elements.

What is Auto Layout?

Page 7: Auto Layout on Xcode 5

1. It allows for more flexible layouts when dealing with multiple screen sizes, interface orientations, and languages.

2. You won’t do a lot of coding for layout.

Why use Auto Layout?

Page 8: Auto Layout on Xcode 5

Auto Layout on Xcode 5Auto Layout is enabled by default. When you add an object in your xib in Xcode 4, constraints(T-bars) appear immediately. We tend to disable auto layout because it looks frustrating.

Apple knows it. That’s why in Xcode 5, even though Auto Layout is enabled, and you’re not creating any constraints, the T-bars will not appear.

Page 9: Auto Layout on Xcode 5

Auto Layout on Xcode 5

In Auto Layout, if you set your button in that position and added constraints, you are telling xcode that the button is on the top-right position of the view, regardless of its orientation or screen size.

When setting the button position without auto-layout, you can set its position in the xib. But what if you change the orientation to landscape or you have a different screen size? You have to write code to set its position.

Page 10: Auto Layout on Xcode 5

Add Constraints

There are 3 ways to add constraints.● From the toolbar● Using Interface Builder● By Dragging Objects

Page 11: Auto Layout on Xcode 5

Toolbar

Editor Menu is used for auto layout. Just select the object on the view and select from the editor which constraints you want to add.

Page 12: Auto Layout on Xcode 5

Interface BuilderOn the bottom part of IB, there are four buttons for auto layout.● Align● Pin ● Resolve Auto Layout Issues● Resizing BehaviorIt has the same content as the Editor Menu

Page 13: Auto Layout on Xcode 5

Interface Builder

Align

Page 14: Auto Layout on Xcode 5

Interface Builder

Pin

Page 15: Auto Layout on Xcode 5

Interface Builder

Resolve Auto Layout Issues

Page 16: Auto Layout on Xcode 5

Interface Builder

Resizing Behavior

Page 17: Auto Layout on Xcode 5

Dragging Object

You can also add constraints by dragging the object to another object, then a menu will popup and you can select which constraints you want.

Page 18: Auto Layout on Xcode 5

Orange and Blue

Page 19: Auto Layout on Xcode 5

Orange Line

When you add constraints on your object, an orange line will appear. This means that xcode cannot identify or compute the position of that object. Xcode needs more constraints to get more information on that object.

Page 20: Auto Layout on Xcode 5

Blue Line

If the T-bar is blue, it means that xcode has sufficient information about the object.

Page 21: Auto Layout on Xcode 5

Orange LineWhen you manually adjust the position of an object that already has constraints, an orange line will appear. This means that there is an error. You can solve this by using the Resolve Auto Layout in Editor Menu.

Select Editor > Resolve Auto Layout Issues > Update Constraints