I have all my controls laid out in interface builder (many labels, buttons etc). How do I put them all in a scroll view in interface builder so that I can have more space and be able to scroll up and down to reveal more controls? Do I have to do this programatically?
10 Answers
Open the view that has all the controls and labels, etc. (in Interface Builder). Select All. Then under the Editor menu, select Embed In, then Scroll View.
Note: in older Xcode versions, this is under the Layout menu, then Embed Objects In... (scroll view).
-
2this doesn't work as expected. Everything is under a UIScrollView now, but when I load the app, nothing is scrollable. Why? – erotsppa Oct 06 '09 at 02:33
-
10In IB, make sure that the scrollview is small enough so the whole thing is visible in the view when your app runs. In other words its bounds/frame needs to be smaller or equal to the view it resides in. Then, in your app, you need to set the scrollview.contentSize = size of the view with all the buttons, labels, etc. This will be larger than the frame/bounds you set for the scrollview. If everything is done right, then you should be able to scroll with no other work on your part. – mahboudz Oct 06 '09 at 03:20
-
I couldn't get it to work, but how would you layout things below the 480 height in interface builder anyway? It doesn't seem to let you stretch the window – erotsppa Oct 06 '09 at 04:19
-
1I had to make my view as a top level view, where I could have it be as large as I wanted. Then I had to embed them in a scroll view and then move that into my main view. Changing things is a pain. I wish Interface Builder let us open views in their own window instead of the parent view's window. – mahboudz Oct 06 '09 at 06:33
-
I'm not sure if you did the step within your code, say in your view controller's viewDidLoad to set the scrollview.contentSize to be the size of the larger view. That is important. If you still don't get it, do a quick little test app with just one UIImageView inside a UIScrollView and use Apple's samples if you have trouble. – mahboudz Oct 06 '09 at 06:39
My preferred solution, where you don't need to hard-code the size of the contentSize:
NB: you might be able to avoid the source-code parts of this using the trick here: https://stackoverflow.com/a/11239123/153422 - although I haven't tried it yet.
The rest of this trick ... you still need to use anyway
Move all controls into a single UIView (in IB: select all, then go Layout > Embed Objects In ... > View)
Hookup that single UIView to your source code using an IBOutlet property (see below)
IN SOURCE CODE, NOT INTERFACE BUILDER (IB is broken here, it has bugs where it sets the origin of the UIScrollView incorrectly - it tries to center the view. Apple never bothered to check it for basic bugs, sigh): Move the single UIView into a UIScrollView (see code below).
Use sizeThatFits to "automatically" set the correct size.
Code (StackOverflow won't let me put code inside a numbered list. Sigh)
Header file:
/** outlet that you hook up to the view created in step 1 */
@property(nonatomic, retain) IBOutlet UIView *masterView;
Class file:
/** inside your viewDidLoad method */
[scrollview addSubview: masterView]; // step 3
scrollView.contentSize = [masterView sizeThatFits:CGSizeZero]; // step 4
...although I haven't checked this recently, IIRC it works on both 2.x and 3.x
-
This also works fine on 4.x - I'm still using it in apps, with no problems. – Adam Oct 23 '10 at 17:00
-
this works on iOS 4.2 on IPhone. dont forget UIScrollView height should be same or equal to top level UIView for ViewController. I had been fiddling with settings and forgot to change this back. Got confused as it seemed to still bounce to wrong position. – brian.clear Dec 07 '10 at 21:43
-
There is no point calling `sizeThatFits` here. It is sufficient to set the scroll view's `contentSize` to the size of the masterView. – matt Jan 02 '11 at 23:12
-
@matt - IIRC, that didn't work correctly on older SDK versions (although the docs made it sound as though it ought to work). If it works now, great. – Adam Jan 06 '11 at 15:46
-
Thank you for the great idea! I figured that you could format a `UIView` like that and add it to a `UIScrollView` (although I spent a lot of time trying to fit all my content inside a `UIScrollView` too large for its own good), but I never would have though to use the `sizeThatFits:` method! Awesomeness! – msgambel Sep 21 '11 at 06:35
I don't know if it's just me, but I tried to follow the instructions in each of the other answers here and none of them worked. None of the answers included everything needed, each one I guess assuming we know to do something so leaving that part out. I finally figured it out with the help of red artisan. So... I am listing here ALL the necessary steps to get this to work:
In InterfaceBuilder, add a View and then add your controls to it (or if your controls already exist in the main view, you can select all your controls and then go to Editor | Embed In | View, then drag that new View so it is all by itself outside the main view). This View can be any size you like.
In InterfaceBuilder, add a Scroll View to your main view, and size it to take up the whole main view.
Add the code listed below to your UIViewController Header and Class files.
In InterfaceBuilder, hook up the View containing your controls to 'contentView' in the File's Owner. Hook up the Scroll View to 'scrollView' in the File's Owner.
Header File:
@interface MyViewController : UIViewController @property(nonatomic, retain) IBOutlet UIScrollView *scrollView; @property(nonatomic, retain) IBOutlet UIView *contentView;
Class File:
@synthesize scrollView, contentView; - (void)viewDidLoad { [super viewDidLoad]; [self.scrollView addSubview:self.contentView]; self.scrollView.contentSize = self.contentView.bounds.size; } - (void)viewDidUnload { self.scrollView = nil; self.contentView = nil; [super viewDidUnload]; }

- 8,381
- 2
- 55
- 49
Select all the objects you want to put into a scroll view and go to the Layout menu, choose "Embed Objects In" and choose "Scroll View".

- 19,083
- 4
- 59
- 71
Its easy:
First add a scrollview to your view. Change the size of the scrollview (e.g. make it 700 pixels long). Start putting your controls When you want to put/edit controls in the lower (invisble) part, select the scrollview and change the Y-start position to -300. Voila. After editing set the Y-start position back to 0 or whatever it was.

- 41
- 1
-
Alternatively, you can just increase the size, then decrease it again. Scrolling would be nice, but I'm not surprised that it isn't implemented – Casebash Sep 16 '10 at 11:45
-
If you do this, then you must also manually (re)set the scroll view's "frame" size to the page's view size (the view portal) AND set the "contentSize" to the total content size. The later defaults to 0,0 and if contentSize isn't larger than frame.size then no scrolling will occur. It's a good additional strategy along with the other two, so thanks! – Hari Honor Mar 16 '12 at 18:19
Although this question is very old, I will suggest a workaround I found as I had the same issue and wasn't able to find much help out there:
When in IB, if you want to place objects outside the 420 pixel, just make sure yourself of having selected Unspecified for all of Status Bar, Top Bar, and Bottom Bar for the View that contains the Scroll View with all the objects. This way, you'll be able to manually resize the screen (for the View). Then, you can follow Ximonn's advice on resizing the H value for the Scroll View, having access to all the other objects, working with them and then, undoing changes for H value and then setting the needed Bars.

- 838
- 9
- 15
Important little thing. To scroll big subview (UIImageView for example) in UIScrollView remember, for this subview, uncheck "User Interaction Enabled" checkbox in InterfaceBuilder -> View window. Or do it programatically.
subview.userInteractionEnabled = NO;
Otherwise this subview will stack on screen without any effect.

- 3,651
- 1
- 32
- 34
The selected answer works well for Xcode 3.
However, for Xcode 4, menus have been re-arranged slightly.
To do the same in Xcode 4 select your views then use:
Editor > Embed In > Scroll View

- 80,579
- 89
- 266
- 376
I've been looking for this for a few days, and I finally came across this site with a solution that worked for me.
Basically you have your main view with a UIScrollView object in it. Then another content view with all your content in it. Then you add the content view to the scroll view. And then finally set the size of the scrollview's content size to the size of the content view.

- 4,412
- 3
- 21
- 20
I know, this thread is a bit older... But somebody could find it on google, it's hight ranked. I wrote this little helper Method to get the job done:
- (void)addSubview:(UIView *)theSubView toScrollView:(UIScrollView *)theScrollView
{
[theScrollView addSubview:theSubView];
theScrollView.contentSize = theSubView.bounds.size;
}
You just have to declare two IBOutlet's in the header (e.g. contentView
and scrollView
) and call the method like this, whereever you want to load a UIView
into a UIScrollView
with your sourcecode:
[self addSubview:contentView toScrollView:scrollView];
I called it in viewDidLoad
This method features iOS

- 7,474
- 7
- 59
- 107