Apply custom UIButton styling to all Buttons

Viewed 12282

I want to apply some styling to all my viewcontrollers from multiple storyboards. I have many viewcontrollers so applying the styling to every viewcontroller sounds rather silly. I want to do it in the most code lean way as possible. I was thinking to do it in the AppDelegate file since you can also change your navigationcontroller styling there, but so far no succes. Not even in the slightest way.

So does anyone know how I can do this?

I am using Swift for the app.

The following styling principles have to be applied: cornerRadius, shadowColor, shadowOffset, shadowRadius, shadowOpacity, maskToBounds.

5 Answers

You can define a class and just set it to your button and configure it in the awakeFromNib() method.

@IBOutlet weak var myButton: MyCustomButton!


class MyCustomButton : UIButton{
    override func awakeFromNib() {
        super.awakeFromNib()
        setTitleColor(UIColor.black, for: .normal)
        layer.cornerRadius = 6
        backgroundColor = UIColor.red
        layer.borderWidth = 0.5
        layer.borderColor = UIColor.VFGGray.cgColor
    }
}

This way all your buttons will have the same appearance. Hope it helps

Related