Archives

  • Categories

  • Wiring Up A UIScrollView With Interface Builder And Monotouch

     

    So you want to add a scrollable view to your iPhone/iPad application and you want to use the power of Interface Builder to make it easier. This is how you do it.

    In this example we are going add a vertically scrolling window to an iPhone screen in portrait mode.

    Start by adding your UIScrollView to your view using Interface Builder. It is not possible to actually scroll the view itself within Interface Builder, so select the UIScrollView and move it up so half the view is off the screen. Drag the edge of the screen to make it longer and then add your extra controls to the bottom. Once you are finished drag the view back to its original position so that you can see the top of the screen again.

    Now in Interface Builder select your UIScrollView and look at its size in the Attribute Inspector window. Make a note of the views height, in this example assume it is 756 pixels. Because we don’t want horizontal scrolling in this example the width should be the same as the standard iPhone screen (320 pixels) .

    Finally in Interface Builder make sure you add an outlet between your ViewController and your UIScrollView.

    Go back to your .NET code and open up your view controller. In your ViewDidLoad method override add the following code to set the initial frame and the content size of you scroll view. Notice that the content size has the full dimension of the entire scrollable view eg. 320 pixels wide and 756 pixels high.

    public override void ViewDidLoad()

    {

        scrollView.Frame = new RectangleF(0,0, 320, 460);

     

        scrollView.ContentSize = new SizeF(320, 756);

    }

     

    Now fire up your application in the iPhone Simulator and you should now have a working scrollable view. 

    Tags: , , ,

    2 Responses to “Wiring Up A UIScrollView With Interface Builder And Monotouch”


    1. Brad Moore says:

      Thanks a million :D

    2. Suly Mutsaers says:

      Thanks, just needed that bit of direction..

    Leave a Reply