【发布时间】:2019-04-03 12:55:18
【问题描述】:
我已经配置了一个测试游乐场,其中包含 UITableView 和 UIStackView 的实例作为其标题。
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)
}
【问题讨论】:
-
为什么stackView 会自行调整大小以适应所有标签并使大小为零。 ??我的意思是你为此做了什么额外的工作?
-
我什么都没做,这是您运行我提供的代码时观察到的行为。
-
请清除,您想显示所有标签还是想隐藏一些?
-
我希望 UIStackView 中的所有内容都以全尺寸显示。
-
你必须提到 tableView 的高度,所以这就是问题所在,现在只需尝试
stackView.distribution = .fillEqually tbleView.tableHeaderView?.frame.size = stackView.frame.size如果你的 stackView 在单元格中,那么你可以使用 UIAutoDimention 但你需要给 tableviewheader 八个。
标签: ios uitableview autolayout swift-playground uistackview