UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to activate multiple Auto Layout constraints using activate()

Swift version: 5.6

Paul Hudson    @twostraws   

Using Auto Layout is the best way to create complex layouts that automatically adapt to their environment, but sometimes adding and removing lots of constraints can cause performance problems.

As an example, here’s a simple UIView with some color so you can see it on-screen:

let vw = UIView()
vw.translatesAutoresizingMaskIntoConstraints = false
vw.backgroundColor = .red
view.addSubview(vw)

We could use Auto Layout anchors to give that constraints: stay 20 points from the leading and trailing edges of its superview, be fixed at 100 points in height, and center itself on-screen:

vw.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20).isActive = true
vw.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
vw.heightAnchor.constraint(equalToConstant: 100).isActive = true
vw.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true

However, while that approach is easy to read – and perfectly fine while you’re learning or if you don’t have complex layouts – there is a more efficient way. NSLayoutConstraint has a class method called activate() that activates multiple constraints at once, which should allow Auto Layout to update its entire layout as a single batch.

The code for this is straightforward: just pass in an array of constraints to the activate() method, like this:

NSLayoutConstraint.activate([
    vw.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20),
    vw.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
    vw.heightAnchor.constraint(equalToConstant: 100),
    vw.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor)
])

If you need to deactivate constraints, there’s an equivalent deactivate() method that is used the same way.

Note: Auto Layout is smart enough to bulk actual layout changes even with the isActive approach – i.e., it will only call layoutSubviews() once per view even if you change four constraints – but Apple says that using activate() is definitely more efficient.

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 8.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.6/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.