【问题标题】:Extra Space Between UITableViewSections in iOS 15iOS 15 中表格视图部分之间的额外空间
【发布时间】:2021-11-26 09:51:11
【问题描述】:

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 中的输出:

【问题讨论】:

  • 重复问题...请参阅此处以解决您的问题:stackoverflow.com/a/69350529/6883935
  • @EduardoSanti 不,不是。在您指出的问题中,有人询问其表格视图周围的边距。我询问部分之间的空白。接受的答案包含我的问题的解决方案,但该问题的作者说这不是他们问题的解决方案。
  • 对不起,我看错了。你在使用 heightForHeaderInSection 委托方法吗?
  • 我尝试使用heightForHeaderInSection 和不使用,并尝试使用 0.0 和 1.0 高度 - 它不影响空间。正如我所提到的,Xcode 的视图调试器显示部分之间没有标题(或任何其他视图),只有一个空白区域。而这个空间只出现在 iOS 15 中。
  • 可以分享图片吗?

标签: ios uikit ios15


【解决方案1】:

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

由于该属性仅在 iOS 15 中可用,您可能需要使用可用性块对其进行包装:

if #available(iOS 15.0, *) {
  tableView.sectionHeaderTopPadding = 0.0
}

这是来自问题的原始代码 sn-p,包括必要的更改:

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 中的输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2013-01-09
    • 2018-09-16
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多