Thursday, September 17, 2009

UIScrollView with controls

Apple has created a number of samples on how to use a UIScrollView control. But all of them are for scrolling graphics and not for controls. What I wanted to do was to have a view that was taller than the device and allow the user to scroll through the controls on the page. And I didn't want to use a TableView to do it. I wanted to use IB.

I finally found the right clues in an old discussion forum post. http://discussions.apple.com/thread.jspa?messageID=9060384

Here is the Cliff Notes version.

Create a new ViewController. Open the xib in interface builder. Delete the view control and replace it with a UIScrollView. Be sure to link the UIScrollView as the view for the File's Owner.

Go back to your code and add a UIScrollView reference in your .h. e.g.
@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;

Don't for get to link it up in IB.

Now in your viewDidLoad method, add the following line:

scrollView.contentSize = CGSizeMake(320, 700);

Now you can add all the controls you want to the UIScrollView control over in IB. Labels, buttons, everything.

I'm surprised that there isn't an easier way to do this in IB. And if there is, it isn't obvious to me how to do it - so please share.

No comments:

Post a Comment