如何打印出锚常数的值?例如:
let myButton = UIButton()
myButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 5).isActive = true
myButton.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
myButton.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
myButton.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
print(myButton.topAnchor.constant)//this would print 5
答案 0 :(得分:1)
您错了这个概念。常数用于锚定的约束。锚不具有常量。 约束。因此,您需要做的是维护约束的引用并访问常量。
if (index < 3) return subTriangles[index];
return null;
一个视图只能有一个 let myButtonTopAnchorConstraint = myButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 5)
myButtonTopAnchorConstraint.isActive = true
myButton.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
myButton.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
myButton.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
print(myButtonTopAnchorConstraint.constant) //Prints 5
,但相对于该锚点可以有多个约束。