6

UITableView with multiple sections without section footers is shown with an extra space between sections. Xcode's view debugger shows that it's not a view, but just an empty space.

In my case the behavior is unwanted.

Playing with adding a 1.0/0.0 height footer doesn't help. Neither does changing the table view's style.

Here's a sample code:

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
    }
 
}

Here's output in iOS 14 and iOS 15:

iOS 14 iOS 15

lazarevzubov
  • 1,767
  • 2
  • 14
  • 24
  • Duplicated question... See here to fix your problem: https://stackoverflow.com/a/69350529/6883935 – Eduardo Santi Oct 06 '21 at 10:59
  • @EduardoSanti No, it's not. In the question you pointed out, a person asks about margins around their table view. I ask about blank space between sections. The accepted answer contains solution of my problem, but the author of that question said that it's not a solution for their problem. – lazarevzubov Oct 07 '21 at 03:49
  • I'm sorry, I read wrong. Are you using the heightForHeaderInSection delegate method? – Eduardo Santi Oct 07 '21 at 08:28
  • I tried both using `heightForHeaderInSection` and not, and tried using 0.0 and 1.0 height – it didn't affect the space. As I've mentioned, Xcode's view debugger showed that there's no header (or any other view) between sections, just an empty space. And this space only appeared in iOS 15. – lazarevzubov Oct 08 '21 at 18:31
  • Could you share images please? – Eduardo Santi Oct 08 '21 at 22:16
  • @EduardoSanti Sure! I updated the question with a sample code and the corresponding output in iOS 14 and iOS 15. – lazarevzubov Oct 10 '21 at 14:19

1 Answers1

17

In iOS 15 the property sectionHeaderTopPadding was added. It affects that exact space. The property's default value is automaticDimension. Setting it to 0.0 fixes the problem.

Since the property is only available in iOS 15, you may want to wrap it with an availability block:

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

Here's the original code snippet from the question including necessary changes:

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.
 
}

Here's the output in iOS 15 after the change:

iOS 15 after update

lazarevzubov
  • 1,767
  • 2
  • 14
  • 24