How to prevent Xcode from asking for ‘required’ initializer ‘init(coder:)’ in every subclass of UIViews or UIViewControllers

Code in Swift
1 min readMar 10, 2022

If you’re like me and prefer to create your views and view controller in code rather than using storyboards or nibs, then you know how annoying it is that Xcode still forces you to implement required initializer ‘init(coder:).

Luckily you can use Swift’s @available attribute to not only prevent Xcode from requiring unnecessary initializers, but also from showing them in the autocompletion list.

Here’s how you can do it for UIViewController.

With this in place, we can create a subclass of ViewController and won’t have to implement required initializer ‘init(coder:) in it.

The same principle can be used for any subclass of UIView. The only downside is you have to create a base subclass for all UIView’s subclasses (like UILabel, UIButton, UITextView and so on). But these are rarely subclassed, so creating base classes for just UIViewController and UIView already removes a lot of unnecessary code.

--

--