为什么在所有边缘上固定 UIStackView 会导致不明确的约束错误?
Why does pinning a UIStackView on all edges cause an ambiguous constraint Error?
以下代码尝试以编程方式将 UIStackView 固定在所有边缘上 - 即使其全屏显示。导致此错误的这些看似简单的约束是什么?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stack = UIStackView(arrangedSubviews: [])
view.addSubview(stack)
NSLayoutConstraint.activate([
stack.leadingAnchor.constraint(equalTo: view.leadingAnchor),
stack.trailingAnchor.constraint(equalTo: view.trailingAnchor),
stack.topAnchor.constraint(equalTo: view.topAnchor),
stack.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
}
调试器中打印的约束错误(link 到 WTFAutoLayout):
(
"<NSAutoresizingMaskLayoutConstraint:0x600003407840 h=--& v=--& UIStackView:0x7f97e240f020.width == 0 (active)>",
"<NSLayoutConstraint:0x60000340ccd0 H:|-(0)-[UIStackView:0x7f97e240f020] (active, names: '|':UIView:0x7f97e6002d90 )>",
"<NSLayoutConstraint:0x60000340dd10 UIStackView:0x7f97e240f020.trailing == UIView:0x7f97e6002d90.trailing (active)>",
"<NSLayoutConstraint:0x6000034073e0 'UIView-Encapsulated-Layout-Width' UIView:0x7f97e6002d90.width == 414 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000340dd10 UIStackView:0x7f97e240f020.trailing == UIView:0x7f97e6002d90.trailing (active)>
同时添加以下代码,
stack.translatesAutoresizingMaskIntoConstraints = false
以下代码尝试以编程方式将 UIStackView 固定在所有边缘上 - 即使其全屏显示。导致此错误的这些看似简单的约束是什么?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stack = UIStackView(arrangedSubviews: [])
view.addSubview(stack)
NSLayoutConstraint.activate([
stack.leadingAnchor.constraint(equalTo: view.leadingAnchor),
stack.trailingAnchor.constraint(equalTo: view.trailingAnchor),
stack.topAnchor.constraint(equalTo: view.topAnchor),
stack.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
}
调试器中打印的约束错误(link 到 WTFAutoLayout):
(
"<NSAutoresizingMaskLayoutConstraint:0x600003407840 h=--& v=--& UIStackView:0x7f97e240f020.width == 0 (active)>",
"<NSLayoutConstraint:0x60000340ccd0 H:|-(0)-[UIStackView:0x7f97e240f020] (active, names: '|':UIView:0x7f97e6002d90 )>",
"<NSLayoutConstraint:0x60000340dd10 UIStackView:0x7f97e240f020.trailing == UIView:0x7f97e6002d90.trailing (active)>",
"<NSLayoutConstraint:0x6000034073e0 'UIView-Encapsulated-Layout-Width' UIView:0x7f97e6002d90.width == 414 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000340dd10 UIStackView:0x7f97e240f020.trailing == UIView:0x7f97e6002d90.trailing (active)>
同时添加以下代码,
stack.translatesAutoresizingMaskIntoConstraints = false