【发布时间】:2019-06-19 08:40:37
【问题描述】:
我的UIView 对象有底部约束,我想更改它的值。完成后,视图确实显示正确,但是,我在控制台中看到错误日志;
[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:0x600003cbe670 PSBApp.KeyboardView:0x7fc5f760e010.bottom == UIView:0x7fc5f4503090.bottom + 306 (active)>",
"<NSLayoutConstraint:0x600003c8cbe0 PSBApp.KeyboardView:0x7fc5f760e010.bottom == UIView:0x7fc5f4503090.bottom - 10 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003cbe670 PSBApp.KeyboardView:0x7fc5f760e010.bottom == UIView:0x7fc5f4503090.bottom + 306 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
我的代码是:
private func setupKeyboard(){
keyboard.delegate = self
view.addSubview(keyboard)
keyboardBottomConstraint = keyboard.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: keyboard.expectedHeight())
keyboard.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
keyboardBottomConstraint.isActive = true
}
private func showKeyboard(){
let bottomOffset: CGFloat = 10
keyboardBottomConstraint.constant = -bottomOffset
}
所以,在我setupKeyboard 之后没有错误并且屏幕上没有可见视图,然后,在我调用showKeyboard 之后它变得可见,但出现了错误。
【问题讨论】:
-
看起来您有 两个 底部约束。你有没有可能给
setupKeyboard()打过两次电话? -
@DonMag 是的,我确实做到了,我确实错误地调用了 setupKeyboard() 两次,感谢您的通知,现在问题解决了!
-
我将添加(附加解释)作为答案,以帮助遇到此问题的其他人。
-
@DonMag 是的,虽然它并不完全依赖于 AutoLayout 机制,但我仍然认为错误是可能的,尤其是当您在代码中设置约束时。
标签: ios swift autolayout nslayoutconstraint