如何为约束定义对象的预设高度和宽度(Swift4)
how to define preset height and width of object for constraint (Swift4)
我的代码以编程方式为 uitextfields 创建约束。它有效。我正在尝试优化我的编码实践并尝试找到一种方法来使用预设约束来确定所有文本字段的高度和宽度。如您所见,a1.和a2。是完全相同的高度和宽度。我想看看是否有一种方法可以为每个文本字段调用相同的高度和宽度,而不是总是对其进行编码。
a1.translatesAutoresizingMaskIntoConstraints = false
let leadingc2 = a1.widthAnchor.constraint(equalToConstant: 75)
let trailingC2 = a1.heightAnchor.constraint(equalToConstant: 50)
let topc2 = a1.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: -60)
let bottomc2 = a1.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -275)
a1t = [leadingc2,trailingC2,topc2,bottomc2]
NSLayoutConstraint.activate(a1t)
//
a2.translatesAutoresizingMaskIntoConstraints = false
let leadingc22 = a2.widthAnchor.constraint(equalToConstant: 75)
let trailingC22 = a2.heightAnchor.constraint(equalToConstant: 50)
let topc22 = a2.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 60)
let bottomc22 = a2.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -275)
a2t = [leadingc22,trailingC22,topc22,bottomc22]
NSLayoutConstraint.activate(a2t)
//Make a variable?
let height = 75
//Or you can make a function to shorten the progress
func setHeight (object : AnyObject?){
object.heightAnchor.constraint(equalToConstant : 75).isActive = true
}
//Then do
setHeight(object : label1)
// you can expand the function for height and other things (: or change it to set all of them programmatically really quick
我的代码以编程方式为 uitextfields 创建约束。它有效。我正在尝试优化我的编码实践并尝试找到一种方法来使用预设约束来确定所有文本字段的高度和宽度。如您所见,a1.和a2。是完全相同的高度和宽度。我想看看是否有一种方法可以为每个文本字段调用相同的高度和宽度,而不是总是对其进行编码。
a1.translatesAutoresizingMaskIntoConstraints = false
let leadingc2 = a1.widthAnchor.constraint(equalToConstant: 75)
let trailingC2 = a1.heightAnchor.constraint(equalToConstant: 50)
let topc2 = a1.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: -60)
let bottomc2 = a1.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -275)
a1t = [leadingc2,trailingC2,topc2,bottomc2]
NSLayoutConstraint.activate(a1t)
//
a2.translatesAutoresizingMaskIntoConstraints = false
let leadingc22 = a2.widthAnchor.constraint(equalToConstant: 75)
let trailingC22 = a2.heightAnchor.constraint(equalToConstant: 50)
let topc22 = a2.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 60)
let bottomc22 = a2.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -275)
a2t = [leadingc22,trailingC22,topc22,bottomc22]
NSLayoutConstraint.activate(a2t)
//Make a variable?
let height = 75
//Or you can make a function to shorten the progress
func setHeight (object : AnyObject?){
object.heightAnchor.constraint(equalToConstant : 75).isActive = true
}
//Then do
setHeight(object : label1)
// you can expand the function for height and other things (: or change it to set all of them programmatically really quick