Xcode-有没有一种方法可以将一个组件从一个视图拖动到另一个视图而不丢失它的框架?

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

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

27193 次浏览

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.

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

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

  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.

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.

It works for me on OSX 10.8.2 and Xcode 4.6.

Good luck!

  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.

NOTE: I m on XCode 4.2

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.

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

This way you do not:

  • edit Storyboard file in a text editor

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.

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

  • remove all the children except ones that you wanted to move
  • recreate new parent (P') constraints
  • recreate possible Interface Builder outlets to (SVs')

And from original parent view (P) you:

  • 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

After:

View1

View2

P

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

View3

P'

SVs'-you-want-to-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.

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.

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

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

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

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.

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

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

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