I'm using PureLayout in my iOS app and I'm following the instructions here: https://github.com/PureLayout/PureLayout/wiki/Tips-and-Tricks That is, using a flag called didSetupConstraints and the method updateViewConstraints. But now I would like to add a view inside another random view when user presses a button. The only way to do that I could imagine was creating another flag like didNewViewSetupConstraints and creating another block inside updateViewConstraints, where I use 2 UIView variables to create the new constraints between them. Is that right? Because I think it is a lot of work for just a small thing (adding a subview and its contraints). Note that I cannot create the constraints without installing them because I don´t know the beforehand (the user will tap them). That is the reason I remark RANDOM. I see in sample code number 10, it is used autoRemoveConstraints and autoInstallConstraints to toggle between 2 groups of constraints. Because my 2 views are random, I guess I cannot do something like that, that is the reason I created a flag and 2 variables for these 2 views to setup the constraints inside updateViewConstraints. But I'm sure that is too much. Thanks for any suggestion or idea.
1 Answers
You don't have to create/activate all of your constraints inside of -updateConstraints
. You can create constraints at any time, and activate them as long as both of the views you wish to constrain share a common superview (in other words, the views need to have been added to the same view hierarchy already).
I would recommend creating your default set of static constraints inside of -updateConstraints
as you're doing. Then, in the callback method when the user taps the button, you can create this new view, add it as a subview, and then create & activate the new constraints for it right then and there.
If you remove this view later on, that will remove all of the constraints associated with it automatically. But if you need to adjust the constraints for this new view, you can store a separate reference to the constraints when they are created.

- 30,197
- 11
- 60
- 73
-
If you're interested, here's a talk that goes into some more advanced concepts about when & where to create/activate constraints: https://www.youtube.com/watch?v=taWaW2GzfCI – smileyborg Jan 31 '16 at 00:23
-
Thanks a lot @smileyborg! Actually that was my first idea but I thought it was wrong because I didn't see that in the samples or docs. That is the reason I thought using updateConstraints was mandatory for every constraint. – Ricardo Jan 31 '16 at 00:33
-
@Ricardo It's not mandatory, but it will always be called at the right time in the layout cycle, it can be more efficient in some cases, and it can help you centralize all of your auto layout code in one place which makes it easier to reason about. See the previous video link for a deeper discussion. – smileyborg Jan 31 '16 at 00:35
-
Thanks! Last question @smileyborg When tapping a button, I create a view and constraints using autoCreateAndInstallConstraints. Do I have to call [self.view setNeedsUpdateConstraints]; after autoCreateAndInstallConstraints in the button handler? Inspecting the code autoCreateAndInstallConstraints I don't see is calling that, however without calling setNeedsUpdateConstraints everything is working fine. Thanks. (I'm watching the video btw ) – Ricardo Jan 31 '16 at 00:45
-
You usually don't need `-setNeedsUpdateConstraints`. The that just schedules a call to the `-updateConstraints` method of whatever you send it to. The only exception is that if you're building everything in code with auto layout, you may need to call `-setNeedsUpdateConstraints` once somewhere in your app's view hierarchy to bootstrap the first auto layout pass. – smileyborg Jan 31 '16 at 00:50
-
OK, makes sense. Thanks a lot! – Ricardo Jan 31 '16 at 00:51