UIStackView sizeToFit无法按预期工作

问题描述 投票:0回答:1

我已经配置了一个测试游乐场,其中包含UITableViewUIStackView实例作为其标题。

stackView包含10个UILabels。

问题是在调用stackView.sizeToFit()之后,stackView不会调整自身大小以适应所有标签,并且其大小为零。

我必须手动设置UIStackView的大小来解决这个问题,这违背了UIStackView的目的。

以下是测试Xcode Playground的代码,因此您可以在您的环境中重现它:

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport


class TestTableViewController: UITableViewController {

    private lazy var searchController = UISearchController(searchResultsController: nil)

    private lazy var stackView: UIStackView = {
        let s = UIStackView()
        s.axis = .vertical
        for i in 0...10 {
            let l = UILabel()
            l.text = "text \(i)"
            s.addArrangedSubview(l)
        }

        return s
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
        configureTableView()
        configureSearchController()
    }


    private func configureTableView() {
        tableView.tableHeaderView = stackView
        stackView.sizeToFit()
        tableView.tableHeaderView?.sizeToFit() // Doesn't work!!!!!!!
        tableView.tableHeaderView?.frame.size = CGSize(width: 9, height: 100)
    }


    private func configureSearchController() {
//        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = NSLocalizedString("Choose template",
                                                                   comment: "Choose template")
    }



    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 7
    }


     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

     cell.textLabel?.text = "\(indexPath.row)"

     return cell
     }

}


let nc = UINavigationController(rootViewController: TestTableViewController())

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = nc

手动设置大小可达到预期效果:

private func configureTableView() {
    tableView.tableHeaderView = stackView
    tableView.tableHeaderView?.frame.size = CGSize(width: 0, height: 400)
}

result

ios uitableview autolayout swift-playground uistackview
1个回答
0
投票

表头视图需要一些额外的帮助......这可能对你有用。

private func configureTableView() {
    tableView.tableHeaderView = stackView
    sizeHeaderToFit(tableView: tableView)
}

private func sizeHeaderToFit(tableView: UITableView) {
    if let headerView = tableView.tableHeaderView {
        let height = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
        var frame = headerView.frame
        frame.size.height = height
        headerView.frame = frame
        tableView.tableHeaderView = headerView
        headerView.setNeedsLayout()
        headerView.layoutIfNeeded()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.