代码中设置的自动布局约束未显示在接口生成器中



我有一个自定义视图,它是在界面生成器中设置的,具有顶部、前导、尾部和高度约束。

在我的自定义视图中,我有一个标题和一个按钮。我试图在标题中添加一个底部和中心Y的约束。以及按钮的宽度、高度、底部、前导约束。

当我添加任何约束时,我会在接口生成器中收到警告:

预期宽度=600,高度=68。

实际:宽度=0,高度=0

当我运行代码时,一切都正常,但我在接口生成器中看不到任何东西。

代码:

@IBDesignable
class UIHeader: UIView {
var delegate: HeaderDelegate?
private lazy var titleLable: UILabel = {
    let lbl = UILabel()
    lbl.translatesAutoresizingMaskIntoConstraints = false
    lbl.font = UIFont(name: "Lato-Light", size: 16)
    lbl.text = "Title"
    return lbl
}()
private lazy var backButton: UIButton = {
    let btn = UIButton()
    btn.tintColor = UIColor.lightGrayColor()
    btn.translatesAutoresizingMaskIntoConstraints = false
    
    let image = UIImage(named: "prev")
    if let image = image {
        btn.setImage(image.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)
    }
}
@IBInspectable
var title: String? {
    get {
        return titleLable.text
    }
    set (newTitle) {
        titleLable.text = newTitle
    }
}
}
extension UIHeader {
private func setupView() {
    backgroundColor = UIColor.whiteColor()
    translatesAutoresizingMaskIntoConstraints = false
    addSubview(titleLable)
    addSubview(backButton)
    
    //add shadow
    layer.shadowColor = UIColor(white: 115/255, alpha: 1.0).CGColor
    layer.shadowOpacity = 0.5
    layer.shadowRadius = 8
    layer.shadowOffset = CGSizeMake(0, -1)
    
    NSLayoutConstraint.activateConstraints([
        //Title//
        //center x
        NSLayoutConstraint(item: titleLable, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0),
        //bottom
        NSLayoutConstraint(item: self, attribute: .Bottom, relatedBy: .Equal, toItem: titleLable, attribute: .Bottom, multiplier: 1, constant: 12),
        
        //button//
        //bottom
        NSLayoutConstraint(item: self, attribute: .Bottom, relatedBy: .Equal, toItem: backButton, attribute: .Bottom, multiplier: 1, constant: 4),
        
        //leading
        NSLayoutConstraint(item: backButton, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 0),
        
        //width
        NSLayoutConstraint(item: backButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: 40),
        
        //height
        NSLayoutConstraint(item: backButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: 40)
        ])
}
}

我还尝试添加以下限制:

addConstraint(NSLayoutConstraint)

搞不清出了什么问题。

感谢

我删除了

    translatesAutoresizingMaskIntoConstraints = false

一切都很好。

最新更新