I am creating UI programatically. I am initialising the UI and adding it as subview in loadView()
method. I have categorized method like:
initUI()
initConstraints()
initStyle()
I am looking for correct override method where each of these can be placed in the UIViewController
.
override func loadView() {
super.loadView()
initUI()
}
func initUI() {
view = UI.view(frame: UIScreen.main.bounds)
view.addSubview(scrollView)
}
func initConstraints() { // Where to place this?
NSLayoutConstraint.activate([]) // ..
}
Where to place initConstraints()
?
The important thing is that the views controlled by the constraints have been added to the view hierarchy before the constraints are created.
If you are creating and activating the constraints in initConstraints()
, you should call it immediately after adding the views to the view hierarchy which is done in loadView()
or viewDidLoad()
.
You can either call initConstraints()
at the end of initUI()
or after the call to initUI()
.