Swift 无法解释'|';性格
我需要你的帮助,因为我不明白我的自动约束是怎么回事。我的开关的限制使应用程序崩溃。当我移除它们时,效果很好。 这是我收到的错误消息: 无法解释“|”字符,因为相关视图没有superview H:|-100-[v0(35)]| 谢谢你的帮助 这是我的密码:Swift 无法解释'|';性格,swift,swift2,autolayout,Swift,Swift2,Autolayout,我需要你的帮助,因为我不明白我的自动约束是怎么回事。我的开关的限制使应用程序崩溃。当我移除它们时,效果很好。 这是我收到的错误消息: 无法解释“|”字符,因为相关视图没有superview H:|-100-[v0(35)]| 谢谢你的帮助 这是我的密码: class selectionCustomCell: UITableViewCell{ var label: UILabel = { let attribution = UILabel() attribu
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")
}
}
注意
label
和switchView
的声明方式之间的区别:label
初始化为闭包的输出,第一次引用闭包时执行闭包switchView
是一个计算属性,每次引用它时都会调用该属性的getter,这意味着您在-SetupView
中引用的版本与之前在上一节中调用的-addSubview
不同。由于它们不属于视图层次结构,因此视觉格式无效
如果您使开关视图
的声明与标签
的声明匹配,则您的代码应按预期工作:
var switchElement: UISwitch = { // note the assignment operator here
let sL = UISwitch()
// ...
return sL
}() // note the invocation of the block here