ios - 动画约束时无法同时满足约束

标签 ios swift animation autolayout

这是我的代码:

    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

关于ios - 动画约束时无法同时满足约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52898145/

相关文章:

c++ - (SFML)按下键时播放器构造函数未更新为正确的动画

ios - 将 UITableView 委托(delegate)和数据源与 tableViewController 分离

ios - 如何在 Core Data Swift 的关系中建立简单的连接

python - 使用 matplotlib 转换多边形的动画

ios - 使用 IQKeyboardManagerSwift 不显示下一个/上一个按钮

ios - 从字典的 NSMutableArray 获取字典

javascript - 有没有更好的方法来实现这个CSS动画而不需要Javascript?

iOS Dev - 手动添加 Storyboard

ios - 使用 super View 的按钮将特定的 subview 加载到容器中

objective-c - 声明并实现带有 block 的 iSO 方法,但没有其他参数