28

I have an iOS app in which i have created some view-controller in storyboard, the view-controller's view has some subviews. Now I want to add some scrollview in that view and want to move all those subviews in that scrollview.But the problem is when i drag those views to scrollview, interface builder is centering those subview, all the position information is lost.

I don't want to let interface builder to do that. Isn't there any proper way to solve this problem? I have already searched about this problem and found these two solutions but none is useful in my case.

Adding a subview to the current view without messing up positioning of objects in the view I am unable to use this solution because, this is adding a view into another view while i want to add scrollview.

XCode - Is there a way to drag a component from one view to another without losing its frame? Also this solution is not helpful for me because, storyboard file is messed up and corrupted when i did this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
iMemon
  • 1,095
  • 1
  • 12
  • 21

2 Answers2

62

Yes there is an easy way to do this:

Step 1: Select your views

enter image description here

Step 2: Go Editor > Embed In > Scroll View

enter image description here

Done!

enter image description here

PS. This is a very handy way to group views in fact. You can embed any views into a 'container' view, move it wherever you like (even cross-scene) retaining relative position information and then you could keep it as a group or unembed them.

Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • Great Answer! Thankyou so much :) Just a little issue i have found is when i embed subview in scrollview, the scrollview width becomes 340 instead of 320. And due to this every subview orgin.x becomes origin.x+10. When i manually change size of scrollview width from 340 to 320, the subview's orgin.x is still origin.x+10. So i have to manually change orgin.x value. Is there a solution for this also? Thanks again for your great technique :) – iMemon Jun 06 '13 at 09:05
  • 1
    Hi, I'm not sure I understand this correctly but you must note two things: 1. Every `UIView`'s frame is relative to its parent view (so when you embed views the frames are reflecting their position relatively to the scrollview). 2. When embedding views that way, IB adds automatically a padding according to Apple's HIG. If you want to resize a 'container' view without changing the positions of its subviews you just uncheck "Autoresize subviews" in IB. I hope that this makes sense... – Alladinian Jun 06 '13 at 09:19
  • Yes you are right. You have understood it the way i want. I already know about first point. Regarding you 2nd point "IB adds automatically a padding according to Apple's HIG.", what i should do when i dont want this behavior? – iMemon Jun 06 '13 at 09:34
  • I suppose you uncheck "Autoresize subviews" (on the container) and set a new size (that way you will not affect the subviews while resizing) – Alladinian Jun 06 '13 at 10:07
  • Actually the problem is IB adds padding according to Apple's HIG to every subview, which i dont want, i have manually changed those values, but just for learning purpose i want to know how to say IB for not adding any padding. I think it is not possible may be, please confirm it. Thanks. – iMemon Jun 06 '13 at 10:28
  • I do not think that this is the case. You can see from my screenshot that only the scrollview was padded to accommodate the subviews (their 'absolute' position was left intact), but yes I don't think that this behavior can be changed through IB. – Alladinian Jun 06 '13 at 10:36
  • 2
    Oh to have known this months ago.. Thank you so much. Frustration lead me to this answer and it is so nice to have frustration lead to learning! – chadkouse Feb 18 '14 at 01:46
  • @chadkouse It's always great to see old answers still helping people :) – Alladinian Feb 18 '14 at 09:13
  • Excellent answer it really solved my problem thanks alot for saving my time – Zagham Arshad Oct 05 '17 at 05:18
4

If anyone comes across this question trying to find a way to do this programmatically (as I did), here's what I ended up using:

for (UIView *view in [_viewAdd subviews]) {
    [_viewMain addSubview:view];
}

Note: addSubview removes the view from it's previous superview so there's no need to code for the removal

CharlesA
  • 4,260
  • 2
  • 25
  • 31