Skip to main content

UIViewControllers

Since the macro is not available for view controllers, you need to manually manage the view hierarchy and override viewDidLayoutSubviews. In the example below, note how viewDidLayoutSubviews insets the view.bounds by the safeAreaInsets and then applies the frame to the body.

final class MyViewController: UIViewController {

var body: Layout {
...
}

override func viewDidLoad() {
super.viewDidLoad()
view.addSubviews {
view1
view2
}
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let layoutRect = view.bounds.inset(by: view.safeAreaInsets)
body.applyFrame(layoutRect)
}
}

@QuickLayout view in a view controller

@QuickLayout
class MyView: UIView {
var body: Layout {
...
}
}

class ViewController: UIViewController {

let myView = MyView()

override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myView)
}

var body: Layout {
ZStack {
myView
}
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
body.applyFrame(view.bounds.inset(by: view.safeAreaInsets))
}
}