Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UIButton Class for Reuse

Tags:

ios

uibutton

Been looking online for a while now and haven't gotten a straight answer on how this is done.

In my app I need a "Next" button that will reside on many ViewControllers. I want this next button to be the same (bordered, same background color, same custom font, same font size, same button size).

//Next Button Properties
[self.nextButtonOutlet setFrame:CGRectMake(120, 454, 80, 30)];
[[self.nextButtonOutlet layer] setBorderWidth:2.0f];
[[self.nextButtonOutlet layer] setBorderColor:UIColorFromRGB(0xbbffd6).CGColor];
self.nextButtonOutlet.titleLabel.font = [UIFont fontWithName:@"GothamRnd-Light" size:22.0];
[self.nextButtonOutlet setTitle:@"next" forState:UIControlStateNormal];
[self.nextButtonOutlet setBackgroundColor:UIColorFromRGB(0x72CE97)];
[self.nextButtonOutlet setTitleColor:UIColorFromRGB(0xbbffd6) forState:UIControlStateNormal];

It seems silly to code all that up in every view controller.

Anyone have a solution to this?

like image 601
lr100 Avatar asked Aug 09 '26 16:08

lr100


1 Answers

class ReusableButton: UIButton {

override init(frame: CGRect) {
    super.init(frame: frame)
    self.layer.borderWidth = 1
    self.layer.borderColor = UIColor.lightGray.cgColor
    self.backgroundColor = .blue
    self.titleLabel?.font = UIFont(name: "fontName", size: fontSize)
    self.setTitle(title: "save", for: .normal)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

Now while creating new buttons you just declare

var myBtn = ReusableButton()

Hope this helps. Happy Coding :]

like image 145
theNoobDev10 Avatar answered Aug 11 '26 07:08

theNoobDev10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!