How to add objects to a UIScrollView that extend beyond UIView from Storyboard?

I want to have several buttons and other objects in a long UIScrollView in my app. In storyboard, I added a UIScrollView to fill the entre view, and then created an IBOutlet in my .h file. I synthesized the scroller in my .m file, and then used the following code to start the scroller:

@synthesize scroller = _scroller;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [_scroller setScrollEnabled:YES];
    [_scroller setContentSize:CGSizeMake(640, 3000)];
}

So now I need to know how to actually add things, such as buttons, to the area of the scroller that extends below what you can see in the view. My problem is that as I add butons to my view in storyboard, I can only add things to what you can see in the view, and therefore need to know how to add buttons to part that I will scroll to!

Hopefully this is clear. Thanks for all your help!


Solution 1:

UPDATE

I have posted a screencast that walks through this technique step-by-step.

ORIGINAL

The easiest way to handle this is simply to make the view in your storyboard taller. When the app runs, any of the normal container view controllers (UINavigationController, UITabBarController, UISplitViewController, or even UIViewController when it's the root view controller of its window) will resize the view to fit on the screen and scroll.

Here's an example of how to set it up in Xcode:

xcode procedure for resizing a storyboard view

I changed the view controller's size from “Inferred” to “Freeform”. Then I changed its view's height from 460 to 800. (By the way, control-shift-click gives you a menu of all objects under the cursor.)

Here's what happens when I run it in the simulator:

simulator running the example app

As you can see, the view hierarchy was resized to fit the screen, but the subviews of the UIScrollView weren't repositioned, and the scroll view set its content size appropriately. (That may only work properly with autolayout, though...)

Solution 2:

So, an example to you how to add a Button:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"Cool title" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(50, 700, 100, 100)];
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn];

You just need to set the frame of the view that you want to add and after that add it as a subview in your scroll.