UITableView
显示没有节页脚的多个节,节之间有额外的空间。 Xcode 的视图调试器显示它不是视图,而只是一个空白区域。
就我而言,这种行为是不受欢迎的。
添加 1.0/0.0 高度页脚并没有帮助。更改表视图也不行style
.
这是示例代码:
import UIKit
final class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = .yellow
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView()
header.backgroundColor = .green
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20.0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = .blue
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 30.0
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footer = UIView()
footer.backgroundColor = .red
return footer
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20.0
}
}
以下是 iOS 14 和 iOS 15 中的输出:
在 iOS 15 中该属性sectionHeaderTopPadding
加入。它会影响那个确切的空间。该属性的默认值为automaticDimension
。将其设置为 0.0 可以解决该问题。
由于该属性仅在 iOS 15 中可用,因此您可能需要用可用性块包装它:
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0.0
}
这是问题的原始代码片段,包括必要的更改:
import UIKit
final class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = .yellow
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0.0
}
}
// The rest is without changes.
}
以下是更改后 iOS 15 中的输出:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)