Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra Space Between UITableViewSections in iOS 15

Tags:

ios

uikit

ios15

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

like image 478
lazarevzubov Avatar asked Jul 12 '26 23:07

lazarevzubov


1 Answers

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

like image 88
lazarevzubov Avatar answered Jul 14 '26 13:07

lazarevzubov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!