提问人:lazarevzubov 提问时间:10/6/2021 最后编辑:lazarevzubov 更新时间:10/10/2021 访问量:2693
iOS 15 中 UITableViewSections 之间的额外空间
Extra Space Between UITableViewSections in iOS 15
问:
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 中的输出:
答:
17赞
lazarevzubov
10/6/2021
#1
在 iOS 15 中添加了该属性。它会影响那个确切的空间。该属性的默认值为 。将其设置为 0.0 可解决此问题。sectionHeaderTopPadding
automaticDimension
由于该属性仅在 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 中的输出:
评论
heightForHeaderInSection