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.
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:
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.
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
.
Step 3:
Open the interface file as XML (Right click on the storyboard/xib > Open As > Source Code
).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With