Programmatic Constraints

In Chapter 3, you learned about Auto Layout constraints and how to add them using Interface Builder. In this section, you will learn how to add constraints to an interface programmatically.

Apple recommends that you create and constrain your views in Interface Builder whenever possible. However, if your views are created in code, then you will need to constrain them programmatically. The interface for MapViewController is created programmatically, so it is a great candidate for programmatic constraints.

To learn about programmatic constraints, you are going to add a UISegmentedControl to MapViewController’s interface. A segmented control allows the user to choose between a discrete set of options, and you ...

Get iOS Programming: The Big Nerd Ranch Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.