Is there a way to subclass UIButton
with specifying its UIButtonType
?
I want to use my button class in designer with type already set.
Is there a way to subclass UIButton
with specifying its UIButtonType
?
I want to use my button class in designer with type already set.
Since UIButtonType
is a read-only property, it is not possible to do this.
Only two scenario to subclass UIButton
:
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
Create a button from designer and rename its class
It will auto generate a subclass of UIButton named CustomButton in you app folder ,and you can assign it to other buttons in designer.
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