Useful extensions tutorial

Hi,

Not so much a tutorial, but it would be useful to have a list of all the useful extensions to storyboard.

I found one the other day to adjust the outline of a button that was really helpful, I guess you pros probably have loads of these that you regularly use!

Hi Ian,

Not quite sure what you mean by extensions to storyboard but do you mean, for example, setting a corner radius to the button?

like this (not my code!!)

@IBDesignable extension UIButton {

    @IBInspectable var borderWidth: CGFloat {
        set {
            layer.borderWidth = newValue
        }
        get {
            return layer.borderWidth
        }
    }

    @IBInspectable var cornerRadius: CGFloat {
        set {
            layer.cornerRadius = newValue
        }
        get {
            return layer.cornerRadius
        }
    }

    @IBInspectable var borderColor: UIColor? {
        set {
            guard let uiColor = newValue else { return }
            layer.borderColor = uiColor.cgColor
        }
        get {
            guard let color = layer.borderColor else { return nil }
            return UIColor(cgColor: color)
        }
    }
}

The thing is that IBDesignables and IBInspectables are infinitely variable so the question comes up as to where do you draw the line.

In my view it’s simpler to create a static function to apply styles to a button such as like this inside a class:

class Utilities {

   static func styleFilledButton(_ button: UIButton) {
        
        // Filled rounded corner style
        button.backgroundColor = UIColor.init(red: 48/255, green: 173/255, blue: 99/255, alpha: 1)
        button.layer.cornerRadius = 25.0
        button.tintColor = UIColor.white
    }

}

Then in your ViewController where you setup the UI you would do this:

Utilities.styleFilledButton(signUpButton)

Each to his own I guess.