A common use case that can occur is needing to add a view for a specific size class. For example, wRegular.
We might want to add an extra label to take advantage of the extra space.
My question is where should I add this label, more specifically should we add this label to our view in the method below ?
traitCollectionDidChange
I was thinking of doing something like so
// sudo code
traitCollectionDidChange{
if (wR){
create special view if special view is nil
self.view.addChildView(special View)
set constraints for wRegular size class
} else{
self.view.removeChildView(special View)
deactivate special view constraints (if not nil)
set constraints for ... size class
}
}
There seems to be tons of examples on how to do this in interface builder but I wasn't sure where we would actually initialize the extra view.
I initialized the special view in traitCollectionDidChange
because we might have a device that will never need the special view.
Don't add and remove view every time. Add it only once. Just activate and deactivate constraints based on trait inside traitCollectionDidChange like following code.
Also, as per the code you wrote in else part, if view is removed, the constraints are automatically removed. As you rotate the device, creating and adding the view, adding constraints everytime is a costly task performance wise.