xcode 使用界面生成器复制带有子项的对象,包括“约束” - iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21533611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy objects with sub items including 'constraints' using interface builder - iOS
提问by Royston Yinkore
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.
每当我这样做时,目标对象只是一个大杂烩,修复它很痛苦。
回答by arinmorf
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:
基本上,你不能直接复制/粘贴,但你可以通过一些额外的步骤:
- Select all views, labels etc, you want to move to the new super view
- Editor -> embed in view
- Move the newly created "container" view to the view you wanted to paste to originally
- Select the "container" view. Editor -> unembed. All the elements will be dumped into the super view, maintaining the layout.
- 选择所有视图、标签等,你想移动到新的超级视图
- 编辑器 -> 嵌入视图
- 将新创建的“容器”视图移动到您最初要粘贴到的视图中
- 选择“容器”视图。编辑器 -> 取消嵌入。所有元素都将转储到超级视图中,从而保持布局。
回答by Acey
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.
它并不总是保持约束到位,但我发现移动一组视图并保持位置的一种方法是选择多个视图,然后用鼠标将视图拖到新位置。您甚至可以通过复制/粘贴将保持所有定位的顶级对象,然后在顶级视图对象之间使用鼠标拖动视图组来将视图复制到新文件。
回答by daviddna
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
回答by Seth Schaffner
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
回答by Nikolay Suvandzhiev
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 carefullymodify the interface file's raw XML. Here's how:
我最终做的是非常小心地修改接口文件的原始 XML。就是这样:
!!! Without proper care, you might corrupt your interface file !!!
!!! Have a backup before attempting this !!!
!!! 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.
第 1 步:
使用常规的 Interface Builder,将所有视图从旧视图移动到新的超级视图。如果它们都搞砸了(例如,都集中在新的超级视图中,或者很多 AutoLayout 错误),请不要担心。只要保留了它们的视图层次结构,就可以了。
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
.
第 2 步:
找出您的旧超级视图和新超级视图的对象 ID。这些显示在 Utilities 部分(右侧)中Identity Inspector > Document > Object ID
。ID 看起来像gGH-lW-Bke
.
Step 3:
Open the interface file as XML (Right click on the storyboard/xib > Open As > Source Code
).
第 3 步:
以 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. 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"/>
现在,非常小心地找到旧超级视图的对象 ID 并将其替换为适用的新超级视图的对象 ID 。不要做“全部替换”!您应该只在以下地方更改它:<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
列为firstItem
或的位置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.
如果您需要更加安全并且如果您的设置更加棘手,您可能希望首先(在步骤 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.
第 4 步:
正常打开接口文件(右键单击 > 打开为 > 接口生成器)。验证一切正常,没有警告或错误。
Done.
完毕。