Unable to interpret '|' character

大憨熊 提交于 2019-12-25 07:18:05

问题


I need your help because I dont understand what's going on with my auto constraints. The constraints of my switch make the app crash. When I remove them it work nicely. This is the error message that I get: Unable to interpret '|' character, because the related view doesn't have a superview H:|-100-[v0(35)]|

thx for your help

Here is my code :

class selectionCustomCell: UITableViewCell{
    var label: UILabel = {
        let attribution = UILabel()
        attribution.text = "Nom du label"
        attribution.textColor = UIColor(r: 0, g: 185, b: 255)
        attribution.lineBreakMode = NSLineBreakMode.ByWordWrapping
        attribution.numberOfLines = 0
        attribution.translatesAutoresizingMaskIntoConstraints = false
        return attribution
    }()

  var switchElement: UISwitch{
        let sL = UISwitch()
        sL.setOn(true, animated: true)
        sL.onTintColor = UIColor(r: 0, g: 185, b: 255)
        sL.tintColor = UIColor(r: 0, g: 185, b: 255)
        sL.translatesAutoresizingMaskIntoConstraints = false
        return sL
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Default, reuseIdentifier: reuseIdentifier)
        addSubview(switchElement)
        addSubview(label)
        setupViews()
    }

    func setupViews(){
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": label]))          
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[v0]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": label]))


        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-100-[v0(35)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": switchElement]))


        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[v0(35)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": switchElement]))


    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

回答1:


Note the difference between how label and switchView are declared: label is initialized to the output of a closure, which is executed the first time it is referenced. switchView is a calculated property with a getter that is invoked each time it is referenced, which means that the versions you are referencing in -setupViews aren't the same as the one that you called -addSubview on previously. Since they don't belong to a view hierarchy, the visual format is invalid.

If you make the declaration of switchView match the declaration of label, your code should work as expected:

var switchElement: UISwitch = { // note the equal operator here
    let sL = UISwitch()
    // ...
    return sL
}() // note the invocation of the block here


来源:https://stackoverflow.com/questions/39497571/unable-to-interpret-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!