简体   繁体   English

使用界面生成器复制带有子项的对象,包括“约束” - iOS

[英]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.每当我这样做时,目标对象只是一个大杂烩,修复它很痛苦。

I had this same problem a while ago, let me know if it helps: Preserve position when moving UIView in hierarchy (paste-in-place)不久前我遇到了同样的问题,如果有帮助,请告诉我: 在层次结构中移动 UIView 时保留位置(就地粘贴)

(copied here for reference): (复制到这里供参考):

Original answer by Jay: https://stackoverflow.com/a/16952902/2070758杰伊的原始答案: 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.所有元素都将被转储到超级视图中,保持布局。

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.我不得不将一个非常复杂的 AutoLayout 设置从一个地方复制到另一个地方,所以我正在寻找避免手动重新创建所有约束的方法。

What I ended up doing is to very carefully modify the interface file's raw XML.我最终做的是非常小心地修改接口文件的原始 XML。 Here's how:就是这样:

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

Step 1:第1步:
Using regular Interface Builder, move all the views from their old to their new superview.使用常规的 Interface Builder,将所有视图从旧视图移动到新的超级视图。 Don't worry if they get all messed up (eg all centred in new superview, or a lot of AutoLayout errors).如果它们都搞砸了(例如,都集中在新的超级视图中,或者很多 AutoLayout 错误),请不要担心。 As long as their view hierarchy is preserved, it's fine.只要保留了它们的视图层次结构,就可以了。

Step 2:第2步:
Find out the Object IDs of your old and your new superview.找出旧的和新的超级视图的对象 ID。 These are shown in the Utilities section (right-hand side) Identity Inspector > Document > Object ID .这些显示在 Utilities 部分(右侧) Identity Inspector > Document > Object ID The ID looks something like gGH-lW-Bke . ID 看起来像gGH-lW-Bke

在此处输入图片说明

Step 3:第 3 步:
Open the interface file as XML (Right click on the storyboard/xib > Open As > Source Code ).以 XML 格式打开接口文件(右键单击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 .现在,非常小心地找到旧超级视图的对象 ID 并将其替换为适用的新超级视图的对象 ID。 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 ) (您需要替换旧视图的id ,其中它被列为firstItemsecondItem

And even so, perhaps in your case there are some constraints that should not be changed (eg 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.如果您需要更加安全并且如果您的设置更加棘手,您可能希望首先(在第 1 步之前)列出您将要移动的所有视图的对象 ID 列表,这些视图涉及约束(即将被放弃)超级视图。

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这是 Xcode 10

I found that embedding multiple items into a stack and then copying - even to the same view - created issues,我发现将多个项目嵌入到一个堆栈中,然后复制——甚至到同一个视图——会产生问题,

while just dropping the items into a horizontal or vertical stack and then copy - pasting the whole stack with keyboard shortcuts worked and maintained constraints.而只是将项目放入水平或垂直堆栈中,然后复制 - 使用键盘快捷键粘贴整个堆栈有效并保持约束。

Using XCode 11.4.1使用 XCode 11.4.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM