iOS 15 中 UITableViewSections 之间的额外空间

laz*_*bov 7 uikit ios ios15

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
    }
 
}
Run Code Online (Sandbox Code Playgroud)

以下是 iOS 14 和 iOS 15 中的输出:

iOS 14 iOS 15

laz*_*bov 17

sectionHeaderTopPadding在 iOS 15 中添加了该属性。它会影响那个确切的空间。该属性的默认值为automaticDimension。将其设置为 0.0 可以解决该问题。

由于该属性仅在 iOS 15 中可用,因此您可能需要用可用性块包装它:

if #available(iOS 15.0, *) {
  tableView.sectionHeaderTopPadding = 0.0
}
Run Code Online (Sandbox Code Playgroud)

这是问题的原始代码片段,包括必要的更改:

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.
 
}
Run Code Online (Sandbox Code Playgroud)

以下是更改后 iOS 15 中的输出:

iOS 15 更新后