我有UIView
高度限制。我想隐藏这个视图,所以我将高度约束常量设置为0。
我在扩展UIView
内的代码中设置约束:
_height = self.heightAnchor.constraint(equalToConstant: 50)
NSLayoutConstraint.activate([
_closeBtn.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 5),
_closeBtn.topAnchor.constraint(equalTo: self.topAnchor, constant: 10),
_closeBtn.heightAnchor.constraint(equalTo: _centerLabel.heightAnchor),
_closeBtn.widthAnchor.constraint(equalTo: _closeBtn.heightAnchor),
_centerLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 10),
_centerLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5),
_centerLabel.leadingAnchor.constraint(equalTo: _closeBtn.trailingAnchor),
_centerLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16),
_height
])
现在,如果我更改_height.constant = 0
,我就会收到此错误:
[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:0x146a374c0 UIButton:0x147615b00'X'.width == UIButton:0x147615b00'X'.height (active)>",
"<NSLayoutConstraint:0x146a37530 UIButton:0x147615b00'X'.height == UILabel:0x147615e80.height (active)>",
"<NSLayoutConstraint:0x146a37450 V:|-(20)-[UILabel:0x147615e80] (active, names: '|':InternetConnectionInfoView:0x14742f600 )>",
"<NSLayoutConstraint:0x146a373e0 UILabel:0x147615e80.bottom == InternetConnectionInfoView:0x14742f600.bottom - 5 (active)>",
"<NSLayoutConstraint:0x146a37680 InternetConnectionInfoView:0x14742f600.height == 0 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x146a374c0 UIButton:0x147615b00'X'.width == UIButton:0x147615b00'X'.height (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.
[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:0x146a37530 UIButton:0x147615b00'X'.height == UILabel:0x147615e80.height (active)>",
"<NSLayoutConstraint:0x146a37450 V:|-(20)-[UILabel:0x147615e80] (active, names: '|':InternetConnectionInfoView:0x14742f600 )>",
"<NSLayoutConstraint:0x146a373e0 UILabel:0x147615e80.bottom == InternetConnectionInfoView:0x14742f600.bottom - 5 (active)>",
"<NSLayoutConstraint:0x146a37680 InternetConnectionInfoView:0x14742f600.height == 0 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x146a373e0 UILabel:0x147615e80.bottom == InternetConnectionInfoView:0x14742f600.bottom - 5 (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.
我怀疑,这来自顶部和底部锚被设置为常量值。我曾尝试使用greaterThanOrEual
/ lessThanOrEqual
,但它会破坏布局。
如何解决这个问题(例如隐藏UIView)?我无法设置isHidden,因为其他UIViews锚定在此视图的底部,他们需要移动隐藏。
答案 0 :(得分:1)
这是一个约束冲突,考虑减少约束优先级可以帮助您解决此问题。
let closeBtnTopAnchor = _closeBtn.topAnchor.constraint(greaterThanOrEqualTo: self.topAnchor)
let closeBtnTopAnchorWithLowPriority = _closeBtn.topAnchor.constraint(equalTo: self.topAnchor, constant: 10)
closeBtnTopAnchorWithLowPriority.priority = UILayoutPriorityDefaultHigh
let centerLabelTopAnchor = _centerLabel.topAnchor.constraint(greaterThanOrEqualTo: self.topAnchor)
let centerLabelTopAnchorWithLowPriority = _centerLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 10)
centerLabelTopAnchorWithLowPriority.priority = UILayoutPriorityDefaultHigh
let centerLabelBottomAnchor = _centerLabel.bottomAnchor.constraint(lessThanOrEqualTo: self.bottomAnchor)
let centerLabelBottomAnchorWithLowPriority = _centerLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5)
centerLabelBottomAnchorWithLowPriority.priority = UILayoutPriorityDefaultHigh
NSLayoutConstraint.activate([
_closeBtn.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 5),
closeBtnTopAnchor,
closeBtnTopAnchorWithLowPriority,
_closeBtn.heightAnchor.constraint(equalTo: _centerLabel.heightAnchor),
_closeBtn.widthAnchor.constraint(equalTo: _closeBtn.heightAnchor),
centerLabelTopAnchor,
centerLabelTopAnchorWithLowPriority,
centerLabelBottomAnchor,
centerLabelBottomAnchorWithLowPriority,
_centerLabel.leadingAnchor.constraint(equalTo: _closeBtn.trailingAnchor),
_centerLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16),
_height
])
答案 1 :(得分:0)
如果隐藏UIView就足够了,如何将其alpha
设置为0?