【问题标题】:Expandable cell, self sizing height可扩展单元,自行调整高度
【发布时间】:2017-05-04 17:21:27
【问题描述】:

我制作了动态数量为UILabels 的可扩展视图(根据查询产生的结果计数以编程方式创建)。但是,我无法根据展开视图中有多少 UILabels 来调整展开视图的高度。

使用for loop,我会根据查询后的结果创建尽可能多的标签。

现在我将它们放在这样的视图中:

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

            }

问题是 - 为什么它不根据约束调整视图高度? :)

【问题讨论】:

  • 为什么不使用 UITableView 并在每个单元格中放置一个 UILabel,UILabel 上的自动布局会根据 uilabel 文本高度调整大小。
  • 您打算针对哪个 iOS 版本?这似乎是 UIStackView 的完美用例。
  • @crizzis 最低 iOS 版本为 iOS 9.0

标签: ios swift uitableview uiview uilabel


【解决方案1】:

试试这个:

cell.setNeedsLayout()
cell.layoutIfNeeded()

cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()

lor之后。

【讨论】:

    猜你喜欢
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2015-06-04
    相关资源
    最近更新 更多