Reputation: 2491
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()
?
Upvotes: 0
Views: 167
Reputation: 154621
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()
.
Upvotes: 2
Reputation: 6779
In you constraint setting function, remember to call,
scrollView.translatesAutoResizingMaskIntoConstraints = false
.
Just after you did view.addSubview(scrollView)
, you can call you constraints function.
But yes if you want them to change later, you should set it in viewDidLayoutSubviews()
.
Upvotes: 1