我有一个带按钮的自定义视图。 在故事板中添加视图时出现错误:
无法呈现代理的实例
当我从界面构建器调试视图时,它会在设置按钮的图像时崩溃。
代码:
@IBDesignable
class UIHeader: UIView {
private lazy var backButton: UIButton = {
let btn = UIButton()
btn.tintColor = UIColor.lightGrayColor()
btn.translatesAutoresizingMaskIntoConstraints = false
//CRASH HERE//
btn.setImage(UIImage(named: "prev")!.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
btn.addTarget(self, action: #selector(UIHeader.OnBackButtonClickLister(_:)), forControlEvents: .TouchUpInside)
return btn
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
}
extension UIHeader {
@IBInspectable
var backButtonImage: UIImage? {
get {
return backButton.imageForState(.Normal)
}
set (newImage) {
backButton.setImage(newImage?.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
}
}
}
extension UIHeader {
private func setupView() {
translatesAutoresizingMaskIntoConstraints = false
addSubview(backButton)
}
}