mike vorisis
mike vorisis

Reputation: 2832

Fatal error: use of unimplemented initializer Swift

I wanted to put an image and label on my navigation bar so I decided to do it like this:

class FixedImageNavigationItem: UINavigationItem {

    private let fixedImage : UIImage = UIImage(named: "navlogo")!
    private var imagak : UIImageView!
    private let imageView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 129, height: 40))
    private var labelaki : UILabel!


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let tView = imageView
        imagak = UIImageView(frame: CGRect(x: 2, y: 3, width: 32, height: 35))
        labelaki = UILabel(frame: CGRect(x: 38, y: 10, width: 87, height: 21))
        imagak.image = fixedImage
        labelaki.text = "Prime Help"
        labelaki.textColor = UIColor.white
        imageView.addSubview(labelaki)
        imageView.addSubview(imagak)
        tView.contentMode = .scaleAspectFit
        self.titleView = tView

    }


}

The above code works great on iphone 5 and later, but when I tried to test it on iphone 4s ios 8 I got this error:

fatal error: use of unimplemented initializer 'init(title:)' for class 'Insurance.FixedImageNavigationItem'

Any ideas?

Thanks in advance

Upvotes: 0

Views: 1639

Answers (2)

Arun Balakrishnan
Arun Balakrishnan

Reputation: 1502

You need to override initWithCoder only if it the going to be configured in Storyboard or Xib. In your case most of the elements are already declared as constants. When you override, initWithCoder, you must also override initWithTitle as initWithTitle is the designated intializer for UINavigationItem. Sample code would like something like this.

 class CustomNavItem :UINavigationItem {
    private let fixedImage : UIImage = UIImage(named: "navlogo")!
    private var imagak : UIImageView!
    private let imageView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 129, height: 40))
    private var labelaki : UILabel!

    override init(title: String) {
        super.init(title: title)
        self.configure()

    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.configure()
    }
    func configure() {
        let tView = imageView
        imagak = UIImageView(frame: CGRect(x: 2, y: 3, width: 32, height: 35))
        labelaki = UILabel(frame: CGRect(x: 38, y: 10, width: 87, height: 21))
        imagak.image = fixedImage
        labelaki.text = title
        labelaki.textColor = UIColor.white
        imageView.addSubview(labelaki)
        imageView.addSubview(imagak)
        tView.contentMode = .scaleAspectFit
        self.titleView = tView
    }
}

Upvotes: 1

Puneet Sharma
Puneet Sharma

Reputation: 9484

The error has nothing to do with device type but with iOS versions as init(coder:) method was introduced in iOS 9, and hence the crash.

But the initiaizer init(title:), is available to use in iOS versions above iOS 2.0 and you have nit implemented it on iOS 8, which is what the error is stating.

You should implement init(title:) instead of init(coder:).

Upvotes: 1

Related Questions