Posts Tagged ‘MonoTouch’

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: