How to Init a Uibutton Subclass

Custom UIButton subclass

If you're adding the button in a storyboard, the initialiser that's called is init?(coder aDecoder: NSCoder), not init(frame: CGRect), so you need to add…

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
setGradientBackground()
}

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)

Initialize UIButton with parameters

In Swift its "better" to try to bypass custom subclass initiation as good as possible <- my opinion don't listen to it... :-)

For guys that anyway love it... You could create a convenience initializer, init your stuff here and call a designated initializer, to init the superclass also... below is an example...

class DayButton: UIButton {

var forCreateView: Bool = false

convenience init (forCreateView: Bool) {
self.init()
self.forCreateView = forCreateView
}

}

let myButton: UIButton = DayButton(forCreateView: false) //Usage

UIButton Subclass can't assign target - Swift 4

You don't have to add another button inside func setUpView() as you already inside the button subclass so configure it's properties directly , all the properties you declare are already exists in the button

class RoundedButton: UIButton {

required init?(coder: NSCoder) {
super.init(coder: coder)
}

init(frame: CGRect, colour: UIColor, text: String) {
super.init(frame: frame)

self.backgroundColor = colour
self.setTitle(text, for: .normal)
self.setTitleColor(UIColor.white, for: .normal)
self.layer.cornerRadius = 4
self.addTarget(self, action: #selector(self.wasPressed(_:)), for: .touchUpInside)

}

@objc func wasPressed(_ sender: UIButton) {
print("was pressed")
}

}

//

let addReview = RoundedButton(frame: CGRect(x:8,y:UIScreen.main.bounds.maxY - 56,width: UIScreen.main.bounds.width - 16, height: 48), colour: #colorLiteral(red: 0.9921568627, green: 0.6941176471, blue: 0.2, alpha: 1), text: "Review" )
self.view.addSubview(addReview)

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?

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

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)



Related Topics



Leave a reply



Submit