Swift : Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIView'

Note for required: Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer.

Note for override: You always write the override modifier when overriding a superclass designated initializer, even if your subclass’s implementation of the initializer is a convenience initializer.

Above both notes are referred from: Swift Programming Language/Initialization

Therefore, your subclass of UIView should look similar to the sample below:

class MyView: UIView {
    ...
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    ...
}

According to the latest swift syntax, the init method needs to add methods:

required init?(coder aDecoder: NSCoder) 
{
         fatalError("init(coder:) has not been implemented")
}

Tags:

Ios

Iphone

Swift