How to Set Uibutton Type in Uibutton Subclass

how to set UIButton type in UIButton Subclass

You may find the discussion at CocoaBuilder's thread How to subclass UIButton? helpful, particularly Jack Nutting's suggestion to ignore the buttonType:

Note that this way the buttonType isn't explicitly set to anything,
which probably means that it's UIButtonTypeCustom. The Docs don't
seem to actually specify that, but since that's the 0 value in the
enum, that's likely what happens (and that seems to be the observable
behavior as well)

Change UIButton type in subclass if button created from storyboard

I think it is not possible at runtime. Since it is a read-only method. I think you will get helpful questions from these links.

set UIButton's buttonType programmatically

Change UIButton type programatically

how to set UIButton type in UIButton Subclass

suclassing a UIButton of type .system

You can't override a convenience method and call super convenience method...
As an alternative you can do a static method that return a FormButton of UIButtonType.system type.

class FormButton: UIButton {
class func newButton() -> FormButton {
return FormButton.init(type: .system)
}
}

Use it like this

let button = FormButton.newButton()

how to set UIButton type in UIButton Subclass

You may find the discussion at CocoaBuilder's thread How to subclass UIButton? helpful, particularly Jack Nutting's suggestion to ignore the buttonType:

Note that this way the buttonType isn't explicitly set to anything,
which probably means that it's UIButtonTypeCustom. The Docs don't
seem to actually specify that, but since that's the 0 value in the
enum, that's likely what happens (and that seems to be the observable
behavior as well)

how to set UIButton type in UIButton Subclass

You may find the discussion at CocoaBuilder's thread How to subclass UIButton? helpful, particularly Jack Nutting's suggestion to ignore the buttonType:

Note that this way the buttonType isn't explicitly set to anything,
which probably means that it's UIButtonTypeCustom. The Docs don't
seem to actually specify that, but since that's the 0 value in the
enum, that's likely what happens (and that seems to be the observable
behavior as well)

Subclassing UIButton with specific UIButtonType

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
    Sample Image

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

    Sample Image

    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

Swift UIButton Subclass and change color based on variable

Your isActive property is written incorrectly. It should not be a computed property in the first place. Currently, the getter will just cause infinite recursion and the setter doesn't actually set anything.

The isActive property should be a stored property with a didSet property observer:

@IBInspectable
var isActive: Bool {
didSet {

}
}

Inside didSet, you can just put the last part of commonInit. The first part of commonInit doesn't need to be run every time isActive changes. I recommend you to extract that as a method called updateBorder:

func updateBorder(isActive: Bool) {

if (isActive) {
self.tintColor = ACTIVE_COLOR
self.layer.borderColor = ACTIVE_COLOR.cgColor
} else {
self.tintColor = nil
self.layer.borderColor = UIColor(red:0.69, green:0.72, blue:0.77, alpha:1.0).cgColor
}

}

And then in didSet, you can just call that:

updateBorder(isActive: isActive)

create uibutton subclass

You don’t really want to subclass UIButton. It’s a class cluster, so individual instances will be something like UIRoundRectButton or some other private Apple class. What are you trying to do that requires a subclass?



Related Topics



Leave a reply



Submit