如何将标识符添加到自动布局锚点约束?
How do I add an identifier to auto layout anchor constraints?
我想为我的所有约束添加标识符,以便调试问题。问题是,如果我使用锚点,我不会直接创建所有约束。
我可以创建一个约束:
let constraint = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 60.0)
但我必须将它添加到视图中,而不是 heightAnchor(没有与 UIStackViews 关联的 .addConstraint() 方法)
那么如何将标识符添加到由这些代码行自动生成的约束:
view.heightAnchor.constraint(equalToConstant: 60.0).isActive = true
view.widthAnchor.constraint(equalToConstant: 60.0).isActive = true
锚点应该让编程自动布局更容易,但肯定不会以无法正确调试为代价?
如果我不能添加标识符,我该如何调试我的“无法满足的”约束异常?
看看我在这个中给出的答案。它会给你一个问题的答案。
(there is no .addConstraint() method
是的,有:
NSLayoutConstraint.activate([constraintvariable])
编辑:
好的,如果我理解正确的话:
let vHeightConstraint = self.view.heightAnchor.constraint(equalToConstant: 60.0);
vHeightConstraint.isActive = true
vHeightConstraint.identifier = "Your identifier"
这样你就有了一个约束变量,在调试时可以看到它的值。
你的代码returns你的约束,所以你可以像这样给它添加标识符
let myConstraint = view.heightAnchor.constraint(equalToConstant: 60.0)
myConstraint.identifier = "myIdentifier"
myConstraint.isActive = true
我使用数组 activate/deactivate 它们:
var p = [NSLayoutConstraint]() // portrait constraints
var l = [NSLayoutConstraint]() // landscape constraints
// (an example of this) pin segmentedView to top
p.append(segmentedControl.topAnchor.constraint(equalTo: imageLayout.topAnchor))
p.append(segmentedControl.widthAnchor.constraint(equalToConstant: 300))
p.append(segmentedControl.centerXAnchor.constraint(equalTo: imageLayout.centerXAnchor))
l.append(segmentedControl.topAnchor.constraint(equalTo: imageLayout.topAnchor))
l.append(segmentedControl.widthAnchor.constraint(equalToConstant: 300))
l.append(segmentedControl.centerXAnchor.constraint(equalTo: imageLayout.centerXAnchor))
public func setOrientation(_ p:[NSLayoutConstraint], _ l:[NSLayoutConstraint]) {
NSLayoutConstraint.deactivate(l)
NSLayoutConstraint.deactivate(p)
if self.bounds.width > self.bounds.height {
NSLayoutConstraint.activate(l)
} else {
NSLayoutConstraint.activate(p)
}
}
你明白了....将你的约束移动到一个数组中,并根据需要 activate/deactivate。
我有一个不同的方法使用 Swift 4.2。我希望这可能有用。
extension UIView{
func getConstraint(forAttribute attribute:NSLayoutConstraint.Attribute)->NSLayoutConstraint?{
for aConstraint in constraints{
if aConstraint.firstAttribute == attribute{
return aConstraint
}
}
return nil
}
}
所以你可以做到;
@IBOutlet weak var confirmButton: MainButton!{
didSet{
confirmButton.getConstraint(forAttribute: .height)!.identifier = aID
confirmButton.isEnabled = false
}
}
或
myOtherButton.getConstraint(forAttribute: .height)?.identifier = "newIdentifier"
我想为我的所有约束添加标识符,以便调试问题。问题是,如果我使用锚点,我不会直接创建所有约束。 我可以创建一个约束:
let constraint = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 60.0)
但我必须将它添加到视图中,而不是 heightAnchor(没有与 UIStackViews 关联的 .addConstraint() 方法)
那么如何将标识符添加到由这些代码行自动生成的约束:
view.heightAnchor.constraint(equalToConstant: 60.0).isActive = true
view.widthAnchor.constraint(equalToConstant: 60.0).isActive = true
锚点应该让编程自动布局更容易,但肯定不会以无法正确调试为代价? 如果我不能添加标识符,我该如何调试我的“无法满足的”约束异常?
看看我在这个
(there is no .addConstraint() method
是的,有:
NSLayoutConstraint.activate([constraintvariable])
编辑:
好的,如果我理解正确的话:
let vHeightConstraint = self.view.heightAnchor.constraint(equalToConstant: 60.0);
vHeightConstraint.isActive = true
vHeightConstraint.identifier = "Your identifier"
这样你就有了一个约束变量,在调试时可以看到它的值。
你的代码returns你的约束,所以你可以像这样给它添加标识符
let myConstraint = view.heightAnchor.constraint(equalToConstant: 60.0)
myConstraint.identifier = "myIdentifier"
myConstraint.isActive = true
我使用数组 activate/deactivate 它们:
var p = [NSLayoutConstraint]() // portrait constraints
var l = [NSLayoutConstraint]() // landscape constraints
// (an example of this) pin segmentedView to top
p.append(segmentedControl.topAnchor.constraint(equalTo: imageLayout.topAnchor))
p.append(segmentedControl.widthAnchor.constraint(equalToConstant: 300))
p.append(segmentedControl.centerXAnchor.constraint(equalTo: imageLayout.centerXAnchor))
l.append(segmentedControl.topAnchor.constraint(equalTo: imageLayout.topAnchor))
l.append(segmentedControl.widthAnchor.constraint(equalToConstant: 300))
l.append(segmentedControl.centerXAnchor.constraint(equalTo: imageLayout.centerXAnchor))
public func setOrientation(_ p:[NSLayoutConstraint], _ l:[NSLayoutConstraint]) {
NSLayoutConstraint.deactivate(l)
NSLayoutConstraint.deactivate(p)
if self.bounds.width > self.bounds.height {
NSLayoutConstraint.activate(l)
} else {
NSLayoutConstraint.activate(p)
}
}
你明白了....将你的约束移动到一个数组中,并根据需要 activate/deactivate。
我有一个不同的方法使用 Swift 4.2。我希望这可能有用。
extension UIView{
func getConstraint(forAttribute attribute:NSLayoutConstraint.Attribute)->NSLayoutConstraint?{
for aConstraint in constraints{
if aConstraint.firstAttribute == attribute{
return aConstraint
}
}
return nil
}
}
所以你可以做到;
@IBOutlet weak var confirmButton: MainButton!{
didSet{
confirmButton.getConstraint(forAttribute: .height)!.identifier = aID
confirmButton.isEnabled = false
}
}
或
myOtherButton.getConstraint(forAttribute: .height)?.identifier = "newIdentifier"