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

Viewed 27163

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'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?

9 Answers

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.

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.

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
Related