Hide chapters

Auto Layout by Tutorials

First Edition · iOS 13 · Swift 5.1 · Xcode 11

Section II: Intermediate Auto Layout

Section 2: 10 chapters
Show chapters Hide chapters

Section III: Advanced Auto Layout

Section 3: 6 chapters
Show chapters Hide chapters

7. Layout Guides
Written by Libranner Santos

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

Layout guides are rectangular areas that you can use to create spaces between your views. In the past, you had to create “dummy views” that acted as spacers. These spacers allowed you to have the desired layout for the UI. Well, with Auto Layout and layout guides, dummy views are no longer necessary.

So, what’s the difference between creating dummy views and using layout guides? To answer this question, you’ll look at some of the key benefits of using layout guides, and how they compare to dummy views:

  • Dummy views can interfere with messages that are meant for one of their children. For example, gestures can easily get captured by the wrong view.
  • Creating dummy views results in a higher performance cost since these views are part of the view hierarchy and have to be maintained. On the other hand, layout guides are not a visual element since their job is simply to define a rectangular area in the view hierarchy.
  • Layout guides are created using code; this allows for more customization, especially when you’re creating all of your constraints within code.

Available Layout Guides

UIKit comes with some layout guides out of the box; they will help you create adaptable interfaces. Their main focus is to make it easy for developers to know the available space at any time, since this can vary from one device to another. These layout guides are Safe Area and Layout Margin and Readable Content.

Safe Area layout guide

The iOS SDK comes with several layout guides, but the most used is the Safe Area layout guide. The Safe Area layout guide represents the portion of the screen that is not covered by the navigation bars, tab bars, and toolbars.

Layout Margin guides and Readable Content guides

The Safe Area layout guide isn’t the only popular guide available in the SDK. The two most notable ones are the Layout Margin and the Readable Content guides.

Creating layout guides

You create layout guides in code using the following these steps:

Creating custom layout guides

Load the starter project and open the MessagingApp project. Build and run.

private func setupLayoutInView(_ view: UIView) {
  let layoutGuide = UILayoutGuide()

  nameLabel.translatesAutoresizingMaskIntoConstraints = false
  callButton.translatesAutoresizingMaskIntoConstraints = false
      equalTo: layoutGuide.topAnchor),
      equalTo: layoutGuide.leadingAnchor),
      equalTo: layoutGuide.trailingAnchor),
      equalTo: layoutGuide.bottomAnchor),
      equalTo: nameLabel.centerXAnchor)
  let margins = view.layoutMarginsGuide
      equalTo: photoImageView.bottomAnchor, constant: 5),
      equalTo: margins.leadingAnchor),
      equalTo: margins.trailingAnchor),
      equalTo: margins.bottomAnchor)

Key points

  • Rather than using dummy views, create container layout guides and use them as spacing views.
  • Use Readable Content guides when you need to display text within your app.
  • Use Layout Margin Guide to restraint all the content inside a view to respect margins.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now