我们可以在 swift 中以编程方式定义和设置约束,如下所示。我以四种不同的方式创建了四个标签出口。像下面
var labelone : UILabel = {
var label = UILabel()
label.text = "Stack"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let labeltwo : UILabel = {
let label = UILabel()
label.text = "Overflow"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var labelthree : UILabel = {
let label = UILabel()
label.text = "Confused"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let labelfour: UILabel = {
var label = UILabel()
label.text = "More confused"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func addconstaraints() {
labelone.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
labelone.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
labelone.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
labeltwo.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
labeltwo.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
labeltwo.topAnchor.constraint(equalTo: labelone.bottomAnchor).isActive = true
labelthree.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
labelthree.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
labelthree.topAnchor.constraint(equalTo: labeltwo.bottomAnchor).isActive = true
labelfour.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
labelfour.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
labelfour.topAnchor.constraint(equalTo: labelthree.bottomAnchor).isActive = true
}
一切正常。有什么不同吗?希望您能帮助理解这一点。
let
我知道变量定义和var
变量定义之间的区别。网点也一样吗?怎么看出outlet的区别