Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy objects with sub items including 'constraints' using interface builder - iOS

How do you copy a view or an object with sub objects with constraints to another view?

Whenever I do this, the destination object is just a mish mash and fixing it is a pain.

like image 801
Royston Yinkore Avatar asked Feb 03 '14 17:02

Royston Yinkore


4 Answers

I had this same problem a while ago, let me know if it helps: Preserve position when moving UIView in hierarchy (paste-in-place)

(copied here for reference):

Original answer by Jay: https://stackoverflow.com/a/16952902/2070758

Basically, you can't copy/paste directly, but you can with some extra steps:

  1. Select all views, labels etc, you want to move to the new super view
  2. Editor -> embed in view
  3. Move the newly created "container" view to the view you wanted to paste to originally
  4. Select the "container" view. Editor -> unembed. All the elements will be dumped into the super view, maintaining the layout.
like image 115
arinmorf Avatar answered Oct 11 '22 14:10

arinmorf


It doesn't always keep the constraints in place, but the one way I've found to move a group of views and keep position is to select multiple, then drag the views with your mouse to its new location. You can even copy views to a new file by copy/pasting the top level object which will keep all positioning, then dragging the group of views with the mouse between top level view objects.

like image 29
Acey Avatar answered Oct 11 '22 13:10

Acey


I had to copy a pretty complicated AutoLayout setup from one place to another so I was looking for ways to avoid having to recreate all the constraints manually.

What I ended up doing is to very carefully modify the interface file's raw XML. Here's how:

!!! Without proper care, you might corrupt your interface file !!!
!!! Have a backup before attempting this !!!

Step 1:
Using regular Interface Builder, move all the views from their old to their new superview. Don't worry if they get all messed up (e.g. all centred in new superview, or a lot of AutoLayout errors). As long as their view hierarchy is preserved, it's fine.

Step 2:
Find out the Object IDs of your old and your new superview. These are shown in the Utilities section (right-hand side) Identity Inspector > Document > Object ID. The ID looks something like gGH-lW-Bke.

enter image description here

Step 3:
Open the interface file as XML (Right click on the storyboard/xib > Open As > Source Code).

enter image description here

Now, very carefully, find and replace the Object ID of the old superview to that of the new one where applicable. DO NOT do a 'replace-all'! You should only be changing it in places such as:
<constraint firstItem="gGH-lW-Bke" firstAttribute="top" secondItem="RXG-iN-Ra7" secondAttribute="top" id="u46-2d-oD0"/>

(You need to replace the old view's id where it's listed as either firstItem or secondItem)

And even so, perhaps in your case there are some constraints that should not be changed (e.g. constraints that are not relating at all to the views that you just moved but are related to the old superview)! So thread carefully.

If you need to be extra safe and if your setup is extra tricky, you might want to first (before Step 1) make a list of the Object IDs of all views that you're about to move and that are involved in constraints with the (about-to-be-abandoned) superview.

Step 4:
Open the interface file back as normal (Right click > Open As > Interface Builder). Verify all looks good, there are no warnings or errors.

Done.

like image 1
Nikolay Suvandzhiev Avatar answered Oct 11 '22 12:10

Nikolay Suvandzhiev


I just had this problem , i just copied the parent view to all the objects i wanted to move over and it preserved all constraints! This is Xcode 10

like image 1
daviddna Avatar answered Oct 11 '22 14:10

daviddna