ios Xcode - 有没有办法在不丢失框架的情况下将组件从一个视图拖动到另一个视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11504172/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:15:29  来源:igfitidea点击:

Xcode - Is there a way to drag a component from one view to another without losing its frame?

iosxcodeinterface-builder

提问by aroth

What I'd like to do is drag a component/view from one superview to another in Xcode's interface-builder without having its frame/position be reset.

我想要做的是在 Xcode 的界面构建器中将组件/视图从一个超级视图拖到另一个超级视图,而无需重置其框架/位置。

Xcode's default behavior when doing this appears to be to center the view being moved vertically and horizontally in its new superview, while preserving its dimensions. This is extremely frustrating, as it means that the view needs to be manually repositioned in its new superview. But I had it positioned correctly before I moved it, so I'd like Xcode to just remember all attributes of its frame instead of just its width/height. Is this possible?

Xcode 执行此操作时的默认行为似乎是将垂直和水平移动的视图在其新超级视图中居中,同时保留其尺寸。这非常令人沮丧,因为这意味着视图需要在其新的超级视图中手动重新定位。但是我在移动它之前已经正确定位了它,所以我希望 Xcode 只记住它的框架的所有属性,而不仅仅是它的宽度/高度。这可能吗?

回答by Jay

Another solution:

另一种解决方案:

  1. Select the items you want in your subview, then
  2. (in the tool bar) Editor > Embed In > view type to embed in.
  1. 在子视图中选择您想要的项目,然后
  2. (在工具栏中)编辑器 > 嵌入 > 要嵌入的视图类型。

回答by vedrano

I found something that might help you guys!

我发现的东西,可能会帮助你们!

The task is to regroup "child views" into "parent view" so they become children of parent view hierarchically and retain physical positions on display as before action.

任务是将“子视图”重新组合为“父视图”,以便它们按层次结构成为父视图的子视图,并像之前的操作一样保留显示的物理位置。

First, adjust the parent view to physically covers area of child views. Second, make sure that all children are bellow it in the view list.

首先,调整父视图以物理覆盖子视图的区域。其次,确保所有孩子都在视图列表中。

Now select all children with the mouse and move them e.g. one pixel up and one pixel down (just to say IB there is some change). After that release children and they will magically become children of the parent and keep their positions on display.

现在用鼠标选择所有孩子并移动它们,例如向上一个像素和向下一个像素(只是说 IB 有一些变化)。之后释放孩子,他们将神奇地成为父母的孩子并保持他们的地位。

It works for me on OSX 10.8.2 and Xcode 4.6.

它适用于 OSX 10.8.2 和 Xcode 4.6。

Good luck!

祝你好运!

回答by Stepan Hruda

I managed to save a lot of time spent repositioning stuff and did this:

我设法节省了大量重新定位东西的时间,并做到了:

  1. Add the parent view in Interface Builder on the same level as the would-be subviews.
  2. Open the storyboard in a text editor and copy the would-be subviews inside the subviews tags of the parent view. XCode is going to update once you save it.
  1. 在 Interface Builder 中添加与子视图相同级别的父视图。
  2. 在文本编辑器中打开故事板,并将子视图复制到父视图的子视图标签中。XCode 将在您保存后更新。

Not very elegant though, can't see why XCode doesn't support it with Shift or something.

虽然不是很优雅,但不明白为什么 XCode 不支持 Shift 或其他东西。

回答by Sourangshu Biswas

  1. Select all controls you want to move from one UIView to another UiView(nay be child) or ScrollView

  2. Cut/Copy

  3. Now after dragging new UIView/ScrollView to your existing UIView, Don't click once to select it, instead DOUBLE click on the new UIView/ScrollView and paste all controls.

  4. Distance difference will remain same among all controls, but you may have to reposition controls again. So don't click anywhere until you have repositioned those, just reposition all controls by navigation arrows as those are all selected already, or you may ve to select those again.

  1. 选择要从一个 UIView 移动到另一个 UiView(不是子视图)或 ScrollView 的所有控件

  2. 剪切/复制

  3. 现在将新的 UIView/ScrollView 拖到现有的 UIView 后,不要单击一次以选择它,而是双击新的 UIView/ScrollView 并粘贴所有控件。

  4. 所有控件之间的距离差异将保持不变,但您可能需要再次重新定位控件。因此,在重新定位之前不要单击任何地方,只需通过导航箭头重新定位所有控件,因为它们都已被选中,或者您可能必须再次选择它们。

NOTE: I m on XCode 4.2

注意:我使用的是 XCode 4.2

回答by Yunus Nedim Mehel

I have done something similar to Stepan's solution, without using a storyboard. In the IB while the ViewController's view is open:

我做了一些类似于 Stepan 的解决方案,没有使用故事板。在 IB 中,当 ViewController 的视图打开时:

  1. Create another view in addition to VC's main view
  2. Move all subviews to the second view by dragging them from one view to another (dragging from the list on the left side resets their position) If you cannot select them from the IB using your mouse then select all from the list on the left side then select one final subview from the IB panel using "cmd" button.
  3. Take them back to the final view on the initial main view.
  4. Remove the added view, all set.
  1. 在 VC 的主视图之外创建另一个视图
  2. 通过将所有子视图从一个视图拖到另一个视图,将所有子视图移动到第二个视图(从左侧的列表拖动会重置它们的位置)如果您无法使用鼠标从 IB 中选择它们,则从左侧的列表中选择所有然后使用“cmd”按钮从 IB 面板中选择一个最终子视图。
  3. 将它们带回到初始主视图上的最终视图。
  4. 删除添加的视图,全部设置。

回答by Dhruv Goel

This is the best solution to copy subviews to another view and retaining the positions :

这是将子视图复制到另一个视图并保留位置的最佳解决方案:

  1. Select the items you want in your subview, then
  2. Editor -> Embed In -> View
  3. Copy this View (Cmd+C)
  4. Undo (Cmd+Z) (Since you just wanted the subviews)
  5. Go to the view you want the subviews in and paste it(Cmd+V)
  6. Select the Embedding View that you copy-pasted and Editor -> Unembed
  1. 在子视图中选择您想要的项目,然后
  2. 编辑器 -> 嵌入 -> 查看
  3. 复制此视图 (Cmd+C)
  4. 撤消 (Cmd+Z)(因为您只想要子视图)
  5. 转到您想要子视图的视图并粘贴它(Cmd+V)
  6. 选择您复制粘贴的嵌入视图和编辑器 -> 取消嵌入

Step 6 would remove the embedding view and you'll have copied just the subviews.

第 6 步将删除嵌入视图,您将只复制子视图。

回答by Randika Vishman

Xcode Interface Builder messes when a Parent View is dragged and dropped into another View (UIView, ScrollView, StackView)

将父视图拖放到另一个视图(UIView、ScrollView、StackView)中时,Xcode Interface Builder 会出现混乱

Q:
Embedding a View (which contains many other subviews within it) into a ScrollView or in another top level view is not straight forward with what I have seen so far. What happens soon after is all the subviews might seems to be misplaced due to it couldn't find their original frame.

问:
到目前为止,将视图(其中包含许多其他子视图)嵌入到 ScrollView 或另一个顶级视图中并不简单。不久之后发生的事情是所有子视图似乎都错位了,因为它找不到它们的原始框架。

A:
Follow following steps and you will be able to resolve it easier as possible:

答:
按照以下步骤操作,您将能够更轻松地解决问题:

  1. Select the items you want in your subview, then
  2. Go to menubar, Editor -> Embed In -> View
  3. Copy this new View (Cmd+C) with the subviews (for me currently all the constraints were preserved so far at this point)
  4. Go to the view (be it a ScrollView or may be StackView) you want the subviews to be in and paste (Cmd+V) the copied view
  5. Select the pasted Embedding View (which you newly created previously) and Go to menubar of Xcode, Editor -> Unembed
  1. 在子视图中选择您想要的项目,然后
  2. 转到菜单栏,编辑器 -> 嵌入 -> 查看
  3. 用子视图复制这个新视图(Cmd+C)(对我来说,目前所有的约束都被保留了)
  4. 转到您希望子视图所在的视图(无论是 ScrollView 还是 StackView)并粘贴(Cmd+V)复制的视图
  5. 选择粘贴的嵌入视图(您之前新创建的)并转到 Xcode 的菜单栏,编辑器 -> 取消嵌入

No, not finished yet! Sometimes, you may experience that there are few more UI Constraint related issues, you will have to resolve them accordingly.

不,还没完!有时,您可能会遇到与 UI 约束相关的问题很少,您必须相应地解决它们。

回答by vedrano

I detected another approach. It is basically: Move = Cut + Paste

我发现了另一种方法。它基本上是:移动 = 剪切 + 粘贴

This way you do:

你这样做:

  • get all your subviews to be children of the new parent view (P')
  • keep (almost) all of your constraints in Auto-Layout based Storyboard
  • keep your subview's relative positions (frames) one to another
  • 让所有子视图成为新父视图的子视图 (P')
  • 保持(几乎)所有的基于自动布局故事板的限制
  • 保持你的子视图的相对位置(框架)一个到另一个

This way you do not:

这样你就不会:

  • edit Storyboard file in a text editor
  • 在文本编辑器中编辑 Storyboard 文件

Base the thing is that each view except one (root) in Storyboard has its parent view. Next, when you copy/move multiple subviews, you lose frames and constraints.

基本的事情是,故事板中除了一个(根)之外的每个视图都有其父视图。接下来,当您复制/移动多个子视图时,您会丢失框架和约束

The answer is pretty simple. You make a copy of your subviews (SVs) by copying their parent view (P) into new parent view (P'). This way you may need to recreate only constraints from that parent new view (P') to its new parent view but not for every subview you wanted to move.

答案很简单。您可以通过将子视图 (SV) 的父视图 (P) 复制到新的父视图 (P') 来制作子视图 (SV) 的副本。通过这种方式,您可能只需要重新创建从该父新视图 (P') 到其新父视图的约束,而不是为您想要移动的每个子视图重新创建约束。

After you did make copy of parent view (P) into new one (P'), from that new view (P') you:

在您将父视图 (P) 的副本复制到新视图 (P') 后,从该新视图 (P') 您:

  • remove all the children except ones that you wanted to move
  • recreate new parent (P') constraints
  • recreate possible Interface Builder outlets to (SVs')
  • 删除所有孩子,除了你想移动的孩子
  • 重新创建新的父 (P') 约束
  • 重新创建可能的 Interface Builder 插座到(SV')

And from original parent view (P) you:

从原始父视图 (P) 来看,您:

  • remove all the children that you wanted to move
  • 删除您想要移动的所有孩子

Before:

前:

View1

View2

P

SVs-you-want-to-move

SVs-you-do-not-want-to-move

View3

视图1

视图2

想搬家的SV

SVs-你不想动

视图3

After:

后:

View1

View2

P

SVs-you-do-not-want-to-move

View3

P'

SVs'-you-want-to-move

视图1

视图2

SVs-你不想动

视图3

P'

SVs'-you-want-move

I should stress that this does not generalise well if you have e.g. UIScrollView as a parent view. Then a copy of it would be again a UIScrollView what may not be desirable.

我要强调的是,如果您将 UIScrollView 作为父视图,则这不能很好地概括。然后它的副本将再次成为 UIScrollView ,这可能是不可取的。

Another thing is when you do remove some of the subviews (SVs) in original parent view (P), you may need to recreate some constraints if other (non-moveable subviews) reference them. But you should do that anyway.

另一件事是当您删除原始父视图 (P) 中的某些子视图 (SV) 时,如果其他(不可移动的子视图)引用它们,您可能需要重新创建一些约束。但无论如何你都应该这样做。

回答by neha

What helped me to solve this issue was-

帮助我解决这个问题的是——

  1. Create the desired parent view (scroll view or uiview) in the xib at the root level and resize it accordingly
  2. Copy all the views that you want to be subviews of this parent view and paste them onto the new view you created in step 1
  3. At this point you will have duplicated these views. The newly added views would be a misaligned but still in the same order and at the same distance with respect to each other, align them in the xib to match their bounds with the old copies of the same view (This assumes that the new parent view has same bounds as the old one)
  4. The newly added views will lose some of the constraints, refer to the old views to fix those
  5. Delete the old views from the xib
  1. 在根级别的 xib 中创建所需的父视图(滚动视图或 uiview)并相应地调整其大小
  2. 复制您想要成为该父视图的子视图的所有视图,并将它们粘贴到您在步骤 1 中创建的新视图上
  3. 此时,您将复制这些视图。新添加的视图将是未对齐但仍然以相同的顺序彼此保持相同的距离,将它们在 xib 中对齐以将它们的边界与同一视图的旧副本相匹配(这假设新的父视图与旧的有相同的界限)
  4. 新添加的视图会失去一些约束,请参考旧视图修复这些
  5. 从 xib 中删除旧视图