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:
以上代码在iphone 5及更高版本上运行良好,但当我尝试在iphone 4s ios 8上测试时,我收到此错误:
fatal error: use of unimplemented initializer 'init(title:)' for class 'Insurance.FixedImageNavigationItem'
致命错误:对类'Insurance.FixedImageNavigationItem'使用未实现的初始化程序'init(title :)'
Any ideas?
有任何想法吗?
Thanks in advance
提前致谢
2 个解决方案
#1
1
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.
该错误与设备类型无关,但iOS版本为init(编码器:)方法在iOS 9中引入,因此崩溃。
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.
但是initiaizer init(title :)可以在iOS 2.0以上的iOS版本中使用,你可以在iOS 8上实现它,这就是错误所说的。
You should implement init(title:)
instead of init(coder:)
.
你应该实现init(title :)而不是init(编码器:)。
#2
1
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.
只有在要在Storyboard或Xib中配置initWithCoder时才需要覆盖它。在您的情况下,大多数元素已经声明为常量。覆盖initWithCoder时,还必须覆盖initWithTitle,因为initWithTitle是UINavigationItem的指定初始化程序。示例代码会像这样。
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
}
}
#1
1
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.
该错误与设备类型无关,但iOS版本为init(编码器:)方法在iOS 9中引入,因此崩溃。
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.
但是initiaizer init(title :)可以在iOS 2.0以上的iOS版本中使用,你可以在iOS 8上实现它,这就是错误所说的。
You should implement init(title:)
instead of init(coder:)
.
你应该实现init(title :)而不是init(编码器:)。
#2
1
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.
只有在要在Storyboard或Xib中配置initWithCoder时才需要覆盖它。在您的情况下,大多数元素已经声明为常量。覆盖initWithCoder时,还必须覆盖initWithTitle,因为initWithTitle是UINavigationItem的指定初始化程序。示例代码会像这样。
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
}
}