自定义视图无法呈现代理的实例 - 在设置UIImage时崩溃

时间:2016-03-22 12:35:57

标签: swift uiview interface-builder

我有一个带按钮的自定义视图。 在故事板中添加视图时出现错误:

  

无法呈现代理的实例

当我从界面构建器调试视图时,它会在设置按钮的图像时崩溃。

代码:

    @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)
}
}

0 个答案:

没有答案