Posts Tagged ‘IPhone’

Wiring Up A UIScrollView With Interface Builder And Monotouch

Sunday, July 18th, 2010


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. 

Programatically Creating a UIButton with MonoTouch

Tuesday, June 15th, 2010

This is probably a really basic post but as I am a still learning the MonoTouch Framework and I couldn’t find anywhere on the Internet explaining how to do it. Anyway today I was trying to programmatically create a UIButton in MonoTouch and set the button type.


There is a ButtonType property on the instantiated UIButton object but its read only and I couldn’t see any constructor that took a buttontype.


In the end the answer was quite simple there is a static factory method on the UIButton class that you use to instantiate a new button. The code looks something like this.


   1: var button = UIButton.FromType(UIButtonType.RoundedRect);

   2: button.frame = new RectangleF(100f, 100f, 100f, 100f);

   3: button.SetTitle("click me", UIControlState.Normal);


Technorati Tags: