I am adding some constraints to an UIView programmatically in Swift. My question is when in the ViewController life cycle is it best to add the constraints? Should I add it in viewDidLoad or viewWillAppear, maybe even viewWillLayoutSubviews. Does anyone know if it even matters where I add the constraints?
Best How To :
It doesn't matter, because the constraints are merely a list of instructions as to what will happen at layout time. They do not depend upon the absolute sizes of anything.
viewDidLoad is thus perfectly good, even though at that time things do not yet have their ultimate sizes - because layout time has not yet arrived.
viewDidLoad has one huge advantage that neither of those others has: it is guaranteed to be called only once, while the others can be called many times (you wouldn't want to keep adding the same constraints over and over, would you?).