【发布时间】:2019-02-25 20:52:31
【问题描述】:
在我的应用程序中,我有三个UIViews,其中一个UILabel 和一个UITableView 显示用户。当没有用户时,我想完全隐藏视图。目前我正在尝试将视图 heightAnchor 设置为零,但这会导致此错误:
无法同时满足约束。
"NSLayoutConstraint:0x604000286cc0 LebensfitFirebase.SurePeople:0x7ff0305d5d30.height == 300 (active)>",
"NSLayoutConstraint:0x60c000095b30 LebensfitFirebase.SurePeople:0x7ff0305d5d30.height == 0 (active)>"
将尝试通过打破约束来恢复
NSLayoutConstraint:0x604000286cc0 LebensfitFirebase.SurePeople:0x7ff0305d5d30.height == 300(活动)>
要完成这项工作,我需要注意什么?顺便说一句,我已将 translatesAutoresizingMaskIntoConstraints 设置为 false。
首先我这样设置三个视图:
surePeopleTV.anchor(top: descLabel.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
maybePeopleTV.anchor(top: surePeopleTV.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
nopePeopleTV.anchor(top: maybePeopleTV.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
这是一种在一行中设置约束的自定义方法。使用此方法不会将高度常量设置为 0。
然后在我的程序中,当 tableviews 中的用户更新时,我会关心高度约束:
func teilnehmerLoaded() {
if surePeopleTV.finishedLoading == true && maybePeopleTV.finishedLoading == true && nopePeopleTV.finishedLoading == true {
view.setNeedsUpdateConstraints()
for controller in tableViewControllers {
if controller.users.count > 0 {
let height: CGFloat = 300
controller.heightAnchor.constraint(equalToConstant: height).isActive = true
controller.confBounds()
} else {
controller.heightAnchor.constraint(equalToConstant: 0).isActive = true
}
}
}
}
【问题讨论】:
标签: ios swift layout autolayout constraints