Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move all subviews to container view keeping positions and constraints

Tags:

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?

like image 479
Anastasia Avatar asked Mar 11 '14 16:03

Anastasia


People also ask

How does UIStackView work?

The stack view aligns the first and last arranged view with its edges along the stack's axis. In a horizontal stack, this means the first arranged view's leading edge is pinned to the stack's leading edge, and the last arranged view's trailing edge is pinned to the stack's trailing edge.

How to use horizontal stack view in Swift?

So, open Main. storyboard in Interface Builder, then embed the existing view controller inside a navigation controller and move it across so you can see it fully. Using the object library, drag a Text Field anywhere into your view, then drag a Horizontal Stack View directly below it.


2 Answers

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.

like image 74
Léo Natan Avatar answered Oct 05 '22 18:10

Léo Natan


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).

like image 42
mvds Avatar answered Oct 05 '22 19:10

mvds