Skip to main content

How It Works

QuickLayout supports fast, automatic layout management for UIKit views. This is achieved by prepending @QuickLayout to your view's declaration. @QuickLayout supports a number of features, including automatic layout, subview management, and sizing. It is the recommended way of integrating QuickLayout for UIViews. However, the macro doesn't work with UIViewControllers, for integration with view controllers see this page.

The snippet below demonstrates a simple @QuickLayout view:

import QuickLayout

@QuickLayout
final class MyView: UIView {

private let label = {
let label = UILabel()
label.text = "Hello World!"
return label
}()

private let image = {
let image = UIImageView()
image.image = UIImage(systemName: "globe.americas")
return image
}()

var body: Layout {
HStack {
label
Spacer(8)
image
}
}
}

The @QuickLayout macro adds layoutSubviews and sizeThatFits methods to your view. After macro expansion, the view will look like this:

final class MyView: UIView {

...

var body: Layout {
...
}

// -------- START OF MACRO EXPANSION ---------
override func layoutSubviews() {
super.layoutSubviews()
_QuickLayoutViewImplementation.layoutSubviews(self)
}

override func sizeThatFits(_ size: CGSize) -> CGSize {
_QuickLayoutViewImplementation.sizeThatFits(self, size: size) ?? .zero
}
// -------- END OF MACRO EXPANSION -----------
}

Note:

  • If you provide a custom implementation for layoutSubviews, the @QuickLayout macro will inject the QuickLayout implementation into your method.
  • However, if you provide a custom implementation for sizeThatFits, the @QuickLayout macro will not modify it.