Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 可扩展单元,自调整高度_Ios_Swift_Uitableview_Uiview_Uilabel - Fatal编程技术网

Ios 可扩展单元,自调整高度

Ios 可扩展单元,自调整高度,ios,swift,uitableview,uiview,uilabel,Ios,Swift,Uitableview,Uiview,Uilabel,我创建了一个可扩展视图,动态显示了UILabels(根据查询生成的结果计数按程序创建)。但是,我无法根据展开视图中有多少ui标签来调整展开视图的高度 使用for loop我创建的标签数量与查询后的结果数量相同 现在我把它们放在视图中,如下所示: for (index, _) in timesBetween2Stations.enumerated() { let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, he

我创建了一个可扩展视图,动态显示了
UILabels
(根据查询生成的结果计数按程序创建)。但是,我无法根据展开视图中有多少
ui标签来调整展开视图的高度

使用
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

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

试试这个:

cell.setNeedsLayout()
cell.layoutIfNeeded()

lor

之后,尝试以下操作:

cell.setNeedsLayout()
cell.layoutIfNeeded()


lor

之后,为什么不使用UITableView并在每个单元格中放置一个UILabel,UILabel上的自动布局将根据UILabel文本高度调整大小。您计划将目标设置为哪个iOS版本?这似乎是
UIStackView
@crizzis最小iOS版本为iOS 9.0的完美用例。为什么不使用UITableView并在每个单元格中放置一个UILabel,UILabel上的自动布局将根据UILabel文本高度调整大小。您计划将目标设置为哪个iOS版本?这似乎是
UIStackView
@crizzis最低iOS版本为iOS 9.0的完美用例