Xcode - Is there a way to drag a component from on

2019-01-12 17:36发布

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?

8条回答
放我归山
2楼-- · 2019-01-12 18:13

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.
查看更多
在下西门庆
3楼-- · 2019-01-12 18:26

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
4楼-- · 2019-01-12 18:28

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.

查看更多
放我归山
5楼-- · 2019-01-12 18:31

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.
查看更多
趁早两清
6楼-- · 2019-01-12 18:31

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!

查看更多
闹够了就滚
7楼-- · 2019-01-12 18:31
  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

查看更多
登录 后发表回答