I just created a brand new app project, and I am able to recreate the issue. Something definitely changed with the SDK.
If I use the below code, WITHOUT tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? implemented at all, the typical top grouped UITableView padding is there.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
private var tableView = UITableView(frame: .zero, style: .grouped)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Testing"
navigationItem.largeTitleDisplayMode = .never
let tableView = UITableView(frame: .zero, style: .grouped)
view.addSubview(tableView)
tableView.dataSource = self
tableView.delegate = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuse")
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuse")!
cell.textLabel?.text = "Test"
return cell
}
}
However, if I simply implement that method the padding either gets deleted completely, or gets reduced (see attached screenshots).
For example, returning a simple UILabel gets rid of the padding, as below:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.text = "Test"
return label
}
This definitely wasn't the case on previous Xcode/iOS 15 versions.