I'm using Xcode 5 and autolayout in my project. I have a .xib
file with a lot of subviews and constraints between them. Now I need to create an intermediate fullscreen container view and put all subviews there. So now I have view->subviews
, and I want view->container view->subviews
. When I do this in IB by dragging subviews into container view, they all get centred and all constraints get lost. It's going to be a little hard to restore all constraints manually. Is there a smart way to do this keeping all subviews' positions and constraints?
Asked
Active
Viewed 4,890 times
26
2 Answers
21
Add your container view to the hierarchy (to get an object ID) in Interface Builder and close the view. Edit the .xib
file manually, in a text editor, and move all subviews to be under the container view. Constraints are created in IB with references to IB object IDs, so you will have to replace the constraints from referencing the superview with the container; a quick replace run should be sufficient.

Léo Natan
- 56,823
- 9
- 150
- 195
-
1I needed also to add missing constraints for container view itself, but that was not a problem. Thank you for great solution! – Anastasia Mar 11 '14 at 17:13
-
1It's a nice idea, but unfortunately if, like me, you have a lot of constraints set up, they all have complicated IDs linking them together... I tried to update the IDs so they pointed to the new container view, but most of them broke! Updating them will take hours... Apple you need to sort this out! – jowie May 28 '14 at 08:51
-
1I can't believe this is still necessary... FML. But thank you, saved me re-assigning every bloody layout constraint by hand. – loglesby Oct 17 '17 at 22:35
14
You can do this in Xcode 6 by using cut&paste to a separate view (which you can later drag around to wherever you actually need it).
-
1This is gold, it actually keep all the constraints! Thank you very much. – hardsetting Nov 04 '15 at 10:00
-
-
-