views:

2417

answers:

5

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?

+1  A: 

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".

Marc W
+2  A: 

Open the view that has all the controls and labels, etc. (in Interface Builder). Select All. Then under the Layout menu, select Embed Objects In... (scroll view).

mahboudz
Marc W beat me to it.
mahboudz
this doesn't work as expected. Everything is under a UIScrollView now, but when I load the app, nothing is scrollable. Why?
erotsppa
In 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
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
I 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
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
+3  A: 

My preferred solution, where you don't need to hard-code the size of the contentSize:

  1. Move all controls into a single UIView (in IB: select all, then go Layout > Embed Objects In ... > View)

  2. Hookup that single UIView to your source code using an IBOutlet property (see below)

  3. 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).

  4. 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

Adam
This solution works great. Thanks for posting
JoshHighland
This also works fine on 4.x - I'm still using it in apps, with no problems.
Adam
+2  A: 

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.

Ximonn
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
A: 

Wow. I am prototyping an iPad app that's basically a 5 page form with 100s of controls. UIScrollView would work fine for the app, except there is no way I'm going to try to develop like that in IB. That would be a nightmare. I'm really surprised there isn't some better solution for creating UIScrollViews in IB.

Matt S