我制作了具有动态数量的 UILabels
的可扩展 View (根据查询生成的结果数以编程方式创建)。但是,我无法根据展开 View 中有多少 UILabels
来调整展开 View 的高度。
使用 for 循环
我制作的标签与查询后的结果一样多。
现在我把它们放到 View 中是这样的:
for (index, _) in timesBetween2Stations.enumerated() {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 20))
cell.ExpandableView.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.center = CGPoint(x: cell.ExpandableView.frame.width / 2, y: 0)
label.textAlignment = NSTextAlignment.center
var substationTime = timesBetween2Stations[index]?.time
let substation = timesBetween2Stations[index]?.station
substationTime = substationTime!.removeTrainTimeZeros()
label.text = substation! + " " + substationTime!
cell.ExpandableView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(10)-[label]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label": label, "expandable": cell.ExpandableView]))
if (previousLabel == nil){
cell.ExpandableView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(10)-[label]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label": label, "expandable": cell.ExpandableView]))
}
else {
cell.ExpandableView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[previousLabel]-(10)-[label]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label": label, "previousLabel": previousLabel as Any]))
}
previousLabel = label
}
问题是 - 为什么它不根据约束调整 View 高度? :)
最佳答案
试试这个:
cell.setNeedsLayout()
cell.layoutIfNeeded()
或
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
在 lor
之后。
关于ios - 可扩展单元格,自定高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41226290/