簡體   English   中英

無法同時滿足約束 iOS

[英]Unable to simultaneously satisfy constraints iOS

我有一個被限制在視圖底部的按鈕。 該按鈕最初是隱藏的,直到點擊表視圖中的項目。 桌子上有一個搜索控制器,當點擊它時,我希望讓按鈕移動,使其始終位於鍵盤上方。

    @objc func keyboardWillShow(notification: NSNotification) {
        if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.height {

            startAssessmentBtn.snp.makeConstraints { make in
                make.bottom.equalTo(-keyboardHeight)
            }

            let userInfo = notification.userInfo! as [AnyHashable: Any]
            let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
            let animationCurve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber

            UIView.setAnimationCurve(UIView.AnimationCurve(rawValue: animationCurve.intValue)!)
            UIView.animate(withDuration: animationDuration.doubleValue) {
                self.tableView.setBottomInset(to: keyboardHeight)
                self.view.layoutIfNeeded()
            }
        }
    }

    @objc func keyboardDidHide(notification: NSNotification) {
        if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {

            let userInfo = notification.userInfo! as [AnyHashable: Any]
            let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
            let animationCurve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber

            startAssessmentBtn.snp.makeConstraints { make in
                make.bottom.equalTo(0)
                //make.bottom.equalToSuperview()//.offset(+keyboardHeight)
            }

            UIView.setAnimationCurve(UIView.AnimationCurve(rawValue: animationCurve.intValue)!)
            UIView.animate(withDuration: animationDuration.doubleValue) {
                self.tableView.setBottomInset(to: 0.0)
                self.startAssessmentBtn.setNeedsLayout()
                self.startAssessmentBtn.layoutIfNeeded()
                self.view.layoutIfNeeded()
            }
        }
    }

使用上面的代碼,我可以在第一次打開鍵盤時讓按鈕出現在鍵盤上方。 但是當鍵盤關閉時,按鈕不會返回到屏幕底部。 我收到以下約束錯誤:

2020-04-01 17:38:42.629598+0100 AuditComply 2.0[4550:3372130] [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. 
(
    "<SnapKit.LayoutConstraint:0x2830057a0@AssetTreeViewController.swift#126 UIButton:0x121deacd0.bottom == UIView:0x121deb100.bottom - 320.0>",
    "<SnapKit.LayoutConstraint:0x283005620@AssetTreeViewController.swift#149 UIButton:0x121deacd0.bottom == UIView:0x121deb100.bottom>"
)

Will attempt to recover by breaking constraint 
<SnapKit.LayoutConstraint:0x283005620@AssetTreeViewController.swift#149 UIButton:0x121deacd0.bottom == UIView:0x121deb100.bottom>

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.

所以看來keyboardWillShow 的約束覆蓋了keyboardDidHide 的約束。 我曾嘗試更改約束的優先級,但這也不起作用。

很明顯你在使用 SnapKit。 makeConstraints向現有約束添加約束。

要更新現有約束,您應該使用updateConstraints ,但您應該錨定到同一點,

例如: .equalToSuperview().inset(0).equalToSuperview().inset(keyboardHeight)

如果沒有錨定到同一點的選項,你應該清除以前的約束並做一些新的。 您可以使用.remakeConstraints同時進行,但您應該添加所有其他約束,如前導、尾隨、頂部

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM