在我按下按钮之前,这是我的UIViewController(我没有把它放在那里,因为它不相关):
当我按下按钮时,顶部会出现一个包含两个UIPickerView的UIView。在这个UIView下面是2个UIButton。 Everthing适用于自动布局:
以下是创建约束时的代码:
let okButtonHeight: CGFloat = 53
let okButtonWidth: CGFloat = 53
let leftPickerViewWidth: CGFloat = 80
let leftPickerViewHeight: CGFloat = 200
let margin: CGFloat = 8
// frame
let constraint0 = NSLayoutConstraint(item: okButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0)
let constraint1 = NSLayoutConstraint(item: frameView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0)
// left & right pickerViews
let constraint2 = NSLayoutConstraint(item: leftPickerView, attribute: .Width, relatedBy: .Equal, toItem: rightPickerView, attribute: .Width, multiplier: 1, constant: 0)
let constraint3 = NSLayoutConstraint(item: leftPickerView, attribute: .Height, relatedBy: .Equal, toItem: rightPickerView, attribute: .Height, multiplier: 1, constant: 0)
let constraint4 = NSLayoutConstraint(item: leftPickerView, attribute: .Trailing, relatedBy: .Equal, toItem: rightPickerView, attribute: .Leading, multiplier: 1, constant: -margin)
let constraint5 = NSLayoutConstraint(item: leftPickerView, attribute: .Top, relatedBy: .Equal, toItem: rightPickerView, attribute: .Top, multiplier: 1, constant: 0)
// left pickerView
let constraint6 = NSLayoutConstraint(item: leftPickerView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: leftPickerViewHeight)
let constraint7 = NSLayoutConstraint(item: leftPickerView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: leftPickerViewWidth)
let constraint8 = NSLayoutConstraint(item: leftPickerView, attribute: .Top, relatedBy: .Equal, toItem: frameView, attribute: .Top, multiplier: 1, constant: -margin)
let constraint9 = NSLayoutConstraint(item: leftPickerView, attribute: .Bottom, relatedBy: .Equal, toItem: frameView, attribute: .Bottom, multiplier: 1, constant: margin)
let constraint10 = NSLayoutConstraint(item: leftPickerView, attribute: .Leading, relatedBy: .Equal, toItem: frameView, attribute: .Leading, multiplier: 1, constant: margin)
// right pickerView
let constraint11 = NSLayoutConstraint(item: rightPickerView, attribute: .Trailing, relatedBy: .Equal, toItem: frameView, attribute: .Trailing, multiplier: 1, constant: -margin)
// ok & cancel buttons
let constraint12 = NSLayoutConstraint(item: okButton, attribute: .Top, relatedBy: .Equal, toItem: cancelButton, attribute: .Top, multiplier: 1, constant: 0)
let constraint13 = NSLayoutConstraint(item: okButton, attribute: .Bottom, relatedBy: .Equal, toItem: cancelButton, attribute: .Bottom, multiplier: 1, constant: 0)
let constraint14 = NSLayoutConstraint(item: okButton, attribute: .Width, relatedBy: .Equal, toItem: cancelButton, attribute: .Width, multiplier: 1, constant: 0)
let constraint15 = NSLayoutConstraint(item: okButton, attribute: .Height, relatedBy: .Equal, toItem: cancelButton, attribute: .Height, multiplier: 1, constant: 0)
// ok button
let constraint16 = NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: okButton, attribute: .Trailing, multiplier: 1, constant: 10)
let constraint17 = NSLayoutConstraint(item: okButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: okButtonWidth)
let constraint18 = NSLayoutConstraint(item: okButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: okButtonHeight)
let constraint19 = NSLayoutConstraint(item: okButton, attribute: .Top, relatedBy: .Equal, toItem: frameView, attribute: .Bottom, multiplier: 1, constant: 10)
// cancel button
let constraint20 = NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: cancelButton, attribute: .Leading, multiplier: 1, constant: -10)
view.addConstraint(constraint0)
view.addConstraint(constraint1)
view.addConstraint(constraint12)
view.addConstraint(constraint13)
view.addConstraint(constraint14)
view.addConstraint(constraint15)
view.addConstraint(constraint16)
view.addConstraint(constraint17)
view.addConstraint(constraint18)
view.addConstraint(constraint19)
view.addConstraint(constraint20)
frameView.addConstraint(constraint2)
frameView.addConstraint(constraint3)
frameView.addConstraint(constraint4)
frameView.addConstraint(constraint5)
frameView.addConstraint(constraint6)
frameView.addConstraint(constraint7)
frameView.addConstraint(constraint8)
frameView.addConstraint(constraint9)
frameView.addConstraint(constraint10)
frameView.addConstraint(constraint11)
selectionCurrencyConstraintList = [constraint0, constraint1, constraint2, constraint3, constraint4, constraint5, constraint6, constraint7, constraint8, constraint9, constraint10, constraint11, constraint12, constraint13, constraint14, constraint15, constraint16, constraint17, constraint18, constraint19, constraint20]
frameView.hidden = true
okButton.hidden = true
cancelButton.hidden = true
此时,UIView和两个按钮隐藏在屏幕顶部正上方。
当我按下按钮使其出现时,我执行以下代码:
frameView.hidden = hide
okButton.hidden = hide
cancelButton.hidden = hide
if var constraints = selectionCurrencyConstraintList {
let topFrameConstraint = constraints[0]
view.removeConstraint(topFrameConstraint)
constraints.removeAtIndex(0)
let newTopConstraint: NSLayoutConstraint!
if hide {
newTopConstraint = NSLayoutConstraint(item: okButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0)
} else {
newTopConstraint = NSLayoutConstraint(item: frameView, attribute: .Top, relatedBy: .Equal, toItem: topView, attribute: .Bottom, multiplier: 1, constant: margin)
}
constraints.insert(newTopConstraint, atIndex: 0)
view.addConstraint(newTopConstraint)
}
UIView.animateWithDuration(0.5) {
self.view.layoutIfNeeded()
}
当我用此NSLayoutConstraint(item: okButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0)
替换此约束NSLayoutConstraint(item: frameView, attribute: .Top, relatedBy: .Equal, toItem: topView, attribute: .Bottom, multiplier: 1, constant: margin)
时,它可以正常工作。
当我扭转它时,我遇到了一些无法满足的约束:
2015-05-15 20:17:25.374 CurrencyEx[624:42779] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x17408de30 V:[UIView:0x174186660(50)]>",
"<NSLayoutConstraint:0x174090fe0 V:[_UILayoutGuide:0x1741a8a40]-(0)-[UIView:0x174186660]>",
"<_UILayoutSupportConstraint:0x1740a3e40 V:[_UILayoutGuide:0x1741a8a40(20)]>",
"<_UILayoutSupportConstraint:0x1740a3d80 V:|-(0)-[_UILayoutGuide:0x1741a8a40] (Names: '|':UIView:0x174186590 )>",
"<NSLayoutConstraint:0x170089150 V:[UIButton:0x146e49020(53)]>",
"<NSLayoutConstraint:0x1700891a0 V:[UIView:0x1741868d0]-(10)-[UIButton:0x146e49020]>",
"<NSLayoutConstraint:0x170088d90 V:[UIPickerView:0x146e09ef0(200)]>",
"<NSLayoutConstraint:0x170088e30 V:|-(-8)-[UIPickerView:0x146e09ef0] (Names: '|':UIView:0x1741868d0 )>",
"<NSLayoutConstraint:0x170088e80 UIPickerView:0x146e09ef0.bottom == UIView:0x1741868d0.bottom + 8>",
"<NSLayoutConstraint:0x17008ef60 V:[UIView:0x174186660]-(8)-[UIView:0x1741868d0]>",
"<NSLayoutConstraint:0x174096620 UIButton:0x146e49020.bottom == UIView:0x174186590.top>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x170088e80 UIPickerView:0x146e09ef0.bottom == UIView:0x1741868d0.bottom + 8>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-05-15 20:17:25.377 CurrencyEx[624:42779] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x17408de30 V:[UIView:0x174186660(50)]>",
"<NSLayoutConstraint:0x174090fe0 V:[_UILayoutGuide:0x1741a8a40]-(0)-[UIView:0x174186660]>",
"<_UILayoutSupportConstraint:0x1740a3e40 V:[_UILayoutGuide:0x1741a8a40(20)]>",
"<_UILayoutSupportConstraint:0x1740a3d80 V:|-(0)-[_UILayoutGuide:0x1741a8a40] (Names: '|':UIView:0x174186590 )>",
"<NSLayoutConstraint:0x170089150 V:[UIButton:0x146e49020(53)]>",
"<NSLayoutConstraint:0x1700891a0 V:[UIView:0x1741868d0]-(10)-[UIButton:0x146e49020]>",
"<NSLayoutConstraint:0x17008ef60 V:[UIView:0x174186660]-(8)-[UIView:0x1741868d0]>",
"<NSLayoutConstraint:0x174096620 UIButton:0x146e49020.bottom == UIView:0x174186590.top>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x17408de30 V:[UIView:0x174186660(50)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-05-15 20:17:25.378 CurrencyEx[624:42779] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x174090fe0 V:[_UILayoutGuide:0x1741a8a40]-(0)-[UIView:0x174186660]>",
"<_UILayoutSupportConstraint:0x1740a3e40 V:[_UILayoutGuide:0x1741a8a40(20)]>",
"<_UILayoutSupportConstraint:0x1740a3d80 V:|-(0)-[_UILayoutGuide:0x1741a8a40] (Names: '|':UIView:0x174186590 )>",
"<NSLayoutConstraint:0x170089150 V:[UIButton:0x146e49020(53)]>",
"<NSLayoutConstraint:0x1700891a0 V:[UIView:0x1741868d0]-(10)-[UIButton:0x146e49020]>",
"<NSLayoutConstraint:0x17008ef60 V:[UIView:0x174186660]-(8)-[UIView:0x1741868d0]>",
"<NSLayoutConstraint:0x174096620 UIButton:0x146e49020.bottom == UIView:0x174186590.top>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1700891a0 V:[UIView:0x1741868d0]-(10)-[UIButton:0x146e49020]>
我不明白为什么。它首先与这些约束一起工作。当我修改它们中的一些时它起作用,当我把它们放回去时,它不再起作用了......
有什么想法吗?
答案 0 :(得分:3)
让我们调用你的约束:
ConstraintHide
:okButton
底部与superview top对齐。
ConstraintShow
:frameView
是topView
下的8分。
如果您逐个检查日志中的碰撞约束,您会注意到约束ConstraintHide
和ConstraintShow
同时存在。
这是由您删除/添加约束引起的。
视图中只有ConstraintHide
ConstraintHide
已正确删除,ConstraintShow
已添加
ConstraintShow
未删除,ConstraintHide
已添加
现在问题是为什么第3步没有删除约束?这是因为第2步中存在错误。您的selectionCurrencyConstraintList
未正确更新,因此在步骤3中您尝试删除原始ConstraintHide
约束而不是第2步添加的约束ConstraintShow
这是因为if var constraints = selectionCurrencyConstraintList
制作了数组的副本。所以你必须在最后保存更改。
<强>解决方案强>
添加
selectionCurrencyConstraintList = constraints
到街区的尽头。
话虽如此,您可以通过在Interface Builder中添加所有约束来大幅简化代码,将ConstraintShow
和ConstraintHide
连接为插座,而不是删除/添加它们,只需更改它们的优先级从0
到1000
。