

You can modify the axis and alignment (and set the distribution and spacing) using the Attributes inspector. The system infers the stack’s axis and alignment from the initial relative position of the views. Interface Builder embeds the selected items in a stack view and resizes the stack to its current fitting size based on its contents. Select one or more items in your layout, and then click on the Stack tool. The Stack tool allows you to quickly create a stack view. This lets Auto Layout calculate the correct positions for you.
#INTERFACE BUILDER FOR MAC UPDATE#
Instead, you can roughly set the relative position of the views, add your constraints, and then update the frames. As an added advantage, when you use these tools, you don’t need to precisely place your views before creating the constraint.

Use the Pin and Align tools when you want fine control when making constraints or when you want to make multiple constraints at once. These are the Stack, Align, Pin, and Resolve Auto Layout Issues tools. Interface Builder provides four Auto Layout tools in the bottom-right corner of the Editor window. Using the Stack, Align, Pin and Resolve Tools If you want finer control, review and edit the constraints after you create them, or use the Pin and Align tools.įor more information on Control-dragging constraints, see Adding Layout Constraints by Control-Dragging in Auto Layout Help. If necessary, you can always edit the constraints afterward.Ĭontrol-dragging provides a very quick way to set a constraint however, because the constraint’s values are inferred from the scene’s current layout, it is easy to end up off by a point. If you line up the views based on Interface Builder’s guidelines, you should end up with a reasonable set of constraints. Therefore, you need to position the views carefully before you draw the constraints. Interface Builder creates the constraints based on the views’ current frames. When dragging to or from the document outline, Interface Builder does not filter the list of possible constraints based on the gesture’s direction. This is often useful when trying to draw constraints to hard-to-find items, like the top or bottom layout guides. You can use the Control-drag gesture both on items in the canvas and on icons in the scene’s document outline. Both gestures may include other options (such as setting the view’s relative size). If you drag more or less vertically, you get options to set the vertical spacing, and options to align the views horizontally. If you drag more or less horizontally, you get options to set the horizontal spacing between the views, and options to vertically align the views. Interface Builder intelligently selects the set of constraints based on the items you are constraining and the direction of your drag gesture. When you release the mouse, Interface Builder displays a HUD menu with a list of possible constraints. To create a constraint between two views, Control-click one of the views and drag to the other. As soon as you add one constraint, you are responsible for adding all the constraints needed to create a nonambiguous, satisfiable layout.įor more information on fixing layout warnings and errors, see Debugging Auto Layout. Just keep adding your constraints until your layout is complete. The affected constraints suddenly appear in red, and Xcode generates a number of warnings.ĭon’t panic. Without the prototyping constraints, your layout no longer has enough constraints to uniquely size and position all the views. Never ship an app with prototyping constraints.Īs soon as you create your first constraint, the system removes all the prototyping constraints from the views referred to by the constraint. Use these constraints to quickly visualize and test a user interface, but then replace the implicit constraints with your own explicit constraints.

The app can build and run with the prototyping constraints. When you place a view on the canvas, Interface Builder automatically creates a set of prototyping constraints that define the view’s current size and position relative to the upper left corner. Most developers find that they prefer one approach over the others however, being familiar with all three approaches lets you quickly switch between tools based on the task at hand.įor all three options, start by dragging your views and controls from the Object library onto the scene.

Each of these approaches has its own set of strengths and weaknesses. There are three main options for setting up Auto Layout constraints in Interface Builder: You can control-drag between views, you can use the Pin and Align tools, and you can let Interface Builder set up the constraints for you and then edit or modify the results. Working with Constraints in Interface Builder
