Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 360
Jason Avatar asked Aug 11 '10 13:08

Jason


3 Answers

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.
like image 106
user637394 Avatar answered Oct 13 '22 08:10

user637394


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!

like image 31
northernman Avatar answered Oct 13 '22 09:10

northernman


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

like image 24
MikeR Avatar answered Oct 13 '22 07:10

MikeR