2

Is there a way to subclass UIButton with specifying its UIButtonType?

I want to use my button class in designer with type already set.

Naomak
  • 409
  • 1
  • 4
  • 9
  • You could Subclass it and inside AwakeFromNib() method, you could set the Type for default. But as I know, correct me if I am wrong, there is no real way to use that Button in Designer, expect u see the default Button and set the class to it. But if u want to draw it by yourself and show that in Designer, there is no way – DirtyNative Oct 10 '17 at 13:42
  • Hi, did my answer help you or not ? – ColeX Oct 13 '17 at 01:31
  • `ButtonType` is read only property, so I dont think you can set it. About using in designer, I meant it like you create custom button class and then in designer assign that class to your button. – Naomak Oct 13 '17 at 07:54

1 Answers1

1

Since UIButtonType is a read-only property, it is not possible to do this.

Only two scenario to subclass UIButton :

  1. Create a subclass of UIbutton and create a public method to wrap it's initial method.

    public class MyButton : UIButton
    {
        public static MyButton CreateButton()
        {
            return UIButton.FromType(UIButtonType.Custom) as MyButton;
        }
    }
    

    Usage:

    MyButton button = MyButton.CreateButton();
    

    You can only use it in code not designer in this way

  2. Create a button from designer and rename its class enter image description here

    It will auto generate a subclass of UIButton named CustomButton in you app folder ,and you can assign it to other buttons in designer.

    enter image description here

    But as i mentioned above, UIButtonType is a read-only property , no way to change it once it's set.

    public partial class CustomButton : UIButton
    {
        public CustomButton (IntPtr handle) : base (handle)
        {
            this.ButtonType = UIButtonType.Custom;    //incorrect , read-only 
    
            this.Font = UIFont.SystemFontOfSize(10);  //correct, read-write
        }
    }
    

    RE: Change UIButton type in subclass if button created from storyboard

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • Thanks for nice answer. Yeah, I have found out both possibilities too. I was only wondering, if there is maybe some other way. I want to use it the way you described in second answer, it's sad you cannot do it like that :/ – Naomak Oct 13 '17 at 07:52
  • 1
    @cole-xia-msft Hm, actually, can you elaborate more about casting in your first example? Because I'm getting `null` as result value and if I cast it as `(MyButton)UIButton.FromType(UIButtonType.Custom)`, I'm getting _Specified cast is not valid._ So I'm wondering, how this could worK for you? – Naomak Jun 20 '18 at 13:16
  • 1
    @Naomak My mistake, i read the remark of `UIButton(UIButtonType type);` ,it says `you should avoid subclassing UIButton and then calling into this constructor from your subclass constructor.` so the first solution is also can't be working. – ColeX Jun 21 '18 at 07:43