gpt4 book ai didi

ios - UITableViewCell - 无法同时满足约束

转载 作者:行者123 更新时间:2023-11-28 20:56:59 29 4
gpt4 key购买 nike

我正在使用 SnapKit 以进行约束并以编程方式创建 View 。但是,有一件事我无法理解。

我想将子 UIView 添加到 TableView 单元格的内容 View 中并设置其高度。这已经导致无法满足的约束错误。

我也试过在 TableView 中设置(估计的)rowHeight,但这没有帮助。

2018-07-14 19:21:38.785556+0200[20027:748731] [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:0x6000000b6500@WeatherCell.swift#24 UIView:0x7fa342409240.top == UITableViewCellContentView:0x7fa342413970.top>",
"<SnapKit.LayoutConstraint:0x6000000b7880@WeatherCell.swift#24 UIView:0x7fa342409240.bottom == UITableViewCellContentView:0x7fa342413970.bottom>",
"<SnapKit.LayoutConstraint:0x6000000b8a80@WeatherCell.swift#26 UIView:0x7fa342409240.height == 145.784842967987>",
"<NSLayoutConstraint:0x604000282210 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa342413970.height == 146 (active)>"
)

Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:0x6000000b8a80@WeatherCell.swift#26 UIView:0x7fa342409240.height == 145.784842967987>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

单元格代码:

class WeatherCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

let expandView = UIView()

self.contentView.addSubview(expandView)

expandView.snp.makeConstraints { view in
view.edges.equalToSuperview()

view.height.equalTo(CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * 200)
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

最佳答案

您需要引用 bottom 约束并将其优先级更改为 999/high 但不是 required ,因此删除 edges 约束并单独制作它们

let expandView = UIView()
self.contentView.addSubview(expandView)
expandView.snp.makeConstraints { (view) in
view.bottom.equalTo(self.contentView).priority(999) // can also be .priority(.high)
view.top.left.right.equalTo(self.contentView)
view.height.equalTo(CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * 200)
}

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com