尝试向其添加 stackView 和一些子项,但出现布局错误。我究竟做错了什么?
当 trailinAchors 常数低于leadingAchors 常数时,问题似乎会发生。
self.stackview.axis = .vertical
self.stackview.alignment = .fill
self.stackview.distribution = .fill
self.stackview.spacing = 5
self.addSubview(self.stackview)
self.stackview.translatesAutoresizingMaskIntoConstraints = false
self.stackview.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
self.stackview.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
self.stackview.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
self.stackview.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
// Holder
let holderView = UIView()
holderView.backgroundColor = UIColor.red
self.stackview.addArrangedSubview(holderView)
// Subview
let view3 = UIView()
view3.backgroundColor = UIColor.orange
holderView.addSubview(view3)
view3.translatesAutoresizingMaskIntoConstraints = false
view3.topAnchor.constraint(equalTo: holderView.topAnchor, constant: 0).isActive = true
view3.bottomAnchor.constraint(equalTo: holderView.bottomAnchor, constant: 0).isActive = true
view3.leadingAnchor.constraint(equalTo: holderView.leadingAnchor, constant: 30).isActive = true
view3.trailingAnchor.constraint(equalTo: holderView.trailingAnchor, constant: -30).isActive = true
错误:
2017-06-30 17:38:37.124333+0200 XXXXX[83954:2132661] [LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x61000028b680 H:|-(30)-[UIView:0x7f8dab565720] (active, names: '|':UIView:0x7f8dab566620 )>",
"<NSLayoutConstraint:0x61000028b6d0 UIView:0x7f8dab565720.trailing == UIView:0x7f8dab566620.trailing - 30 (active)>",
"<NSLayoutConstraint:0x61000028b4a0 H:|-(0)-[UIStackView:0x7f8dab569320] (active, names: '|': XXXXX.MultipleBarGraphsView:0x7f8dab561190'What are you doing' )>",
"<NSLayoutConstraint:0x61000028b3b0 UIStackView:0x7f8dab569320.trailing == XXXXX.MultipleBarGraphsView:0x7f8dab561190'Hur ofta svarar du r\U00e4tt?'.trailing (active)>",
"<NSLayoutConstraint:0x61000028b900 '_UITemporaryLayoutWidth' XXXXX.MultipleBarGraphsView:0x7f8dab561190'Hur ofta svarar du r\U00e4tt?'.width == 0 (active)>",
"<NSLayoutConstraint:0x61000028af00 'UISV-canvas-connection' UIStackView:0x7f8dab569320.leading == UIView:0x7f8dab566620.leading (active)>",
"<NSLayoutConstraint:0x61000028bbd0 'UISV-canvas-connection' H:[UIView:0x7f8dab566620]-(0)-| (active, names: '|':UIStackView:0x7f8dab569320 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x61000028b6d0 UIView:0x7f8dab565720.trailing == UIView:0x7f8dab566620.trailing - 30 (active)>
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.
意识到我打电话
layoutIfNeeded()
在函数的底部。当我删除它时,错误消失了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句