我需要你的帮助,因为我不明白我的自动约束是怎么回事.我的开关的限制使应用程序崩溃.当我删除它时,它工作得很好.这是我得到的错误消息:无法解释'|' 字符,因为相关视图没有超视图H:| -100- [v0(35)] |
谢谢你的帮助
这是我的代码:
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")
}
}
Run Code Online (Sandbox Code Playgroud)
注意how label和switchView声明之间的区别:label初始化为闭包的输出,它在第一次被引用时执行. switchView是计算的特性与被每次被引用时,这意味着你引用的版本调用一个getter -setupViews是不一样的,你那叫一个-addSubview先前.由于它们不属于视图层次结构,因此可视格式无效.
如果你声明switchView匹配声明label,你的代码应该按预期工作:
var switchElement: UISwitch = { // note the equal operator here
let sL = UISwitch()
// ...
return sL
}() // note the invocation of the block here
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1008 次 |
| 最近记录: |