Take the 2-minute tour ×

i'm tring to extend UIButton class to add my id property.

How i can do this?

Problem is to create a button you use

var button = UIButton.FromType(UIButtonType.RoundRect);

so, if i've

public class MyButton : UIButton

how i can create my button?


share|improve this question
add comment

2 Answers

up vote 1 down vote accepted
class MyButton : UIButton
public MyButton(RectangleF rect) : base(rect) {}

static MyButton FromType(UIButtonType buttonType)
    var b = new MyButton (new RectangleF(0, 0, 200, 40));
    b.SetTitle("My Button",UIControlState.Normal);

    //additional customization here

    return b;
share|improve this answer
Basically the same as cvista but using a static factory method to make it cleaner –  bayfrontconsulting Jul 15 '10 at 14:55
i used your code, but there is a compilation error: The type MyButton does not contain a costructor that takes 1 arguments. Some ideas? –  Premier Jul 15 '10 at 20:00
See constructor in edit. Sorry about that. –  bayfrontconsulting Jul 15 '10 at 22:09
add comment

Here is how you would create your button:

MyButton button = new MyButton (new RectangleF(0, 0, 250, 37));
button.SetTitle("My Button",UIControlState.Normal);

To Create a button of a type you could try:

MyButton button = new MyButton (UIButtonType.RoundRect);

and see if it returns a button of the correct type which inherits from your MyButton class - but I doubt it. You may have to add the image and size it yourself:

UIImage image = UIImage.FromFile("action.png");

Not sure if there's another way but looking at the docs the UIButton.ButtonType is read only and there's no SetType method so you can only set a UIButton class ButtonType property by instantiating it and passing the type into the constructor.


share|improve this answer
Please explict MyButton class. Thanks –  Premier Jul 15 '10 at 20:01
With your first code suggest, no buttons on screen. –  Premier Jul 15 '10 at 20:44
Ok, i've added a background and now i see buttons. thanks. –  Premier Jul 15 '10 at 21:22
add comment

Your Answer


