3

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
lr100
  • 648
  • 1
  • 9
  • 29

3 Answers3

3
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 :]

theNoobDev10
  • 409
  • 5
  • 6
2

You can create an extension for UIButton and add a new method that returns your custom UIButton.

In objective-c:

+ (UIButton *)nextButton {
    // Create and return your button here
}

or in Swift:

extension UIButton {
    class func nextButton() -> UIButton {
        // Create and return your button here

     return nextButton()
    }
}

then create it using:

UIButton.nextButton()
Juan Boero
  • 6,281
  • 1
  • 44
  • 62
Dash
  • 17,188
  • 6
  • 48
  • 49
  • 1
    Should that be returning the button, not the function in the Swift example? eg. return tempButton, as opposed to return nextButton()? – bwash70 Dec 27 '15 at 08:58
0

There are some options.

  • Use an Interface Builder. You can easily copy-paste your existing button several times, but later it will be hard to change and apply everywhere. Instead, you can store your button in xib and load it. So once you change some property, on next build you will have all buttons updated.
  • Copy instantiated views with a hack like this
  • Make a some UI-factory class, containing all the creation logic.

I would prefer separate xib for that, especially for buttons with rich animations.

Community
  • 1
  • 1
ReDetection
  • 3,146
  • 2
  • 23
  • 40
  • Don't think I can use InterfaceBuilder because I have a custom font. Could be wrong. New to custom fonts also – lr100 May 30 '15 at 18:20
  • Custom font is OK with IB, I do use custom fonts too, but you need to define category. I use that one https://github.com/samwize/UIView-CustomFonts/blob/master/Classes/UIView%2BCustomFonts.m – ReDetection May 30 '15 at 18:47
  • So I just define custom property `fontFamily` on button (label, textfield, etc) of string type and pass font name there. – ReDetection May 30 '15 at 18:49