簡體   English   中英

設置動畫約束時無法同時滿足約束

[英]Unable to simultaneously satisfy constraints when animating constraint

這是我的代碼:

    class AvailableTrainScene: UIView {
          let seeMoreButton: UIButton = {
        let v = UIButton()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .blue
        v.layer.cornerRadius = 20
        v.setTitle("Voir Plus", for: .normal)
        return v
    }()

    var seeMoreBottom: NSLayoutConstraint?

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(seeMoreButton)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func layoutSubviews() {
        NSLayoutConstraint.activate([
            seeMoreButton.heightAnchor.constraint(equalToConstant: 40),
            seeMoreButton.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.8),
            seeMoreButton.centerXAnchor.constraint(equalTo: centerXAnchor)
            ])
            seeMoreBottom = seeMoreButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
        seeMoreBottom?.isActive = false
    }

    func hideSeeMore(_ hide: Bool) {
        if hide {
            seeMoreBottom?.constant = 40
        }else{
            seeMoreBottom?.constant = -8
        }
        UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 4, options: .curveEaseOut, animations: {
            self.layoutIfNeeded()
        }, completion: nil)
    }
}

使用函數hideSeeMore進行動畫設置時出現此錯誤:

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:0x28060dc70 UIButton:0x105534920'Voir Plus'.bottom == DzTrain_2_0.AvailableTrainScene:0x1055300d0.bottom - 8   (active)>",
    "<NSLayoutConstraint:0x2806894a0 UIButton:0x105534920'Voir Plus'.bottom == DzTrain_2_0.AvailableTrainScene:0x1055300d0.bottom + 40   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x2806894a0 UIButton:0x105534920'Voir Plus'.bottom == DzTrain_2_0.AvailableTrainScene:0x1055300d0.bottom + 40   (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.

我嘗試添加一個拖曳約束,一個隱藏,另一個顯示(優先級不同),然后激活/停用它們,但是什么也沒發生? 有人以前有這個嗎?

施加約束

override init(frame: CGRect) {
    super.init(frame: frame)
    addSubview(seeMoreButton)
    NSLayoutConstraint.activate([
        seeMoreButton.heightAnchor.constraint(equalToConstant: 40),
        seeMoreButton.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.8),
        seeMoreButton.centerXAnchor.constraint(equalTo: centerXAnchor)
   ])
   seeMoreBottom = seeMoreButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
   seeMoreBottom?.isActive = false
}

這樣

override func layoutSubviews()

被稱為任何

self.layoutIfNeeded()

這會導致您的常量編輯與此處的新覆蓋沖突

seeMoreBottom = seeMoreButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
seeMoreBottom?.isActive = false

暫無
暫無

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

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