How to set up UIScrollView in Interface Builder with UI elements outside the main iPhone view?

20,159

Solution 1

Double click to open the UIScrollView itself in IB, and increase the size to the size you need or bigger (you can always shrink at runtime). Then just add the elements.

EDIT: Doesn't work - see here instead.

Solution 2

The best workaround I found for this problem (which I consider embarrassing for Interface Builder) is this:

  1. place a "container" UIView inside the UIScrollView, set the size of the container UIView to what is needed (e.g. 320 x 1200) with the inspector and add your content inside that container view. (your buttons, textfields, etc).
  2. set the contentSize for the UIScrollView, in code, to be the same as the size of your container UIView. somewhere in viewDidLoad for example (e.g. scrollView.contentSize = containerView.frame.size;)
  3. To modify content beyond the scrollview's bounds in Interface Builder, you have to drag your container view outside of the scroll view each time, make your modifications, then drag your container view back inside the UIScrollView and build.

Solution 3

This is actually straightforward:

  1. Create a new view controller class e.g. MyScrollViewController

  2. Create a new xib with a UIScrollView as the topmost view, and set the class of the File's Owner to MyScrollView Controller

  3. Set the view attribute of File's Owner to the scroll view

  4. Drag the bottom of the scrollview to create the desired size.

  5. Position your various other UI elements as sub-views of the scroll view

  6. Create and connect an IBOutlet in MyScrollViewController.h for the scroll view, e.g.

    @property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
    
  7. In MyScrollViewController's viewDidLoad method, add the following line of code:

    self.scrollView.contentSize = self.scrollView.frame.size;
    

Th-th-th-that's all folks!

Solution 4

Set up "Content Insets" of "Scroll View Size" on the "Size inspector": Bottom = YourFrameHeight - ScreenHeight. It will allow you to scroll in ranges of top-to-top, bottom-to-bottom of your UIScrollView

Share:
20,159
Jason
Author by

Jason

Updated on July 14, 2022

Comments

  • Jason
    Jason almost 2 years

    I am building a data entry form in my iPhone app, and there are more data fields than will fit on the screen. I figured I should put them into a UIScrollView so that the user can scroll through the form. What's the best way to build this in Interface Builder? I know that I can do it programmatically, but I'd like to do it in Interface Builder if I can. The problem is, how can I lay out UILabels, UITextFields, etc. if they fall outside of the main iPhone screen--in the part of the screen for which the UIScrollView becomes useful?

  • JScarry
    JScarry over 11 years
    This worked for me in portrait mode, but not when I rotated it to landscape. I used this line instead self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, 600.0f); Where the height is slightly larger than the content in the scrollview.
  • jimbob
    jimbob over 10 years
    this answered the question for me. You cannot have your subviews directly underneath the UIScrollView. You need to have a UIView inside the UIScrollView as a container for all your Subviews (I know it is a little stupid).
  • Teffi
    Teffi almost 10 years
    This approach saved me a lot of trouble. Main lesson -> if you are planning to use xib interface for designing the contents of your scrollview, better to do it in a separate uiview then programmatically add it to your scrollview as a whole and then set your contentsize after.