m.y m.y - 4 months ago 26
Swift Question

Convenience initializers for custom uiview loaded from .xib

Is there a way to add convenience initialisers to a custom UIView loaded from a nib?

For example this gist covers the common init method but doesn't have any convenience initializers. I have a few properties that I'd like to set in my custom UIView on initialization.

https://gist.github.com/bwhiteley/049e4bede49e71a6d2e2

Answer

The method you want to override is awakeFromNib (called after initialization) or viewDidLoad (called after the view is loaded). In most cases it's easier to place initialization in viewDidLoad. Your convenience initializers won't be called if you're loaded from a nib (initialization is done by init(coder:) in that case).