ios - 无法以编程方式同时满足约束条件。 swift iOS

标签 ios swift constraints nslayoutconstraint

我正尝试按一下按钮更改约束。

@IBAction func firstButton(_ sender: Any) {
    someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -16).isActive = false
    someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -46).isActive = true
    someTableView.updateConstraints()
}

@IBAction func secondButton(_ sender: Any) {
    someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -46).isActive = false
    someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -16).isActive = true
    someTableView.updateConstraints()
}

一旦两个约束都处于事件状态,我就会出错。他们不会停用:

[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.

[shortened].bottom == [shortened].bottom - 46 (active)>

[shortened].bottom == [shortened].bottom - 16 (active)>

Will attempt to recover by breaking constraint

[shortened].bottom == [shortened].bottom - 16 (active)>

编辑:

这里每个人都有正确答案,对我帮助很大。我刚刚接受了带有示例代码的那个。

谢谢大家!

最佳答案

每次点击都会引起新的冲突

var botCon:NSLayoutConstraint!

//

 botCon = someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -16)
 botCon.isActive = true

//

@IBAction func firstButton(_ sender: Any) {
    botCon.constant = -46
    self.view.layoutIfNeeded()
}

@IBAction func secondButton(_ sender: Any) {
    botCon.constant = -16
    self.view.layoutIfNeeded()
}

关于ios - 无法以编程方式同时满足约束条件。 swift iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51901196/

相关文章:

ios - 处理 Firebase childDeleted 和 childModified 观察者 Swift

java - 如何在 Java 与 swift 中将 long 转换为 int?

ios - Swift URL 字符串编码在 iOS 上崩溃?

sql - SQL 中外键的 ONDELETE 和 ONUPDATE 的默认值是什么?

ios - 将 UINavigationItem 设为 @IBDesignable

ios - UILabel 中的尺寸和线条问题

javascript - 使用 CouchDB 和 IrisCouch 的 iOS 回合制多人游戏

ios - 检测 Sprite 外面/远离 Sprite 的敲击

Rails 中的 Mysql 约束数据库条目

ios - objective-C : animating height constraint change not working