Home > database >  Space between TableViewHeader and Section Header
Space between TableViewHeader and Section Header

Time:11-17

I need help getting rid of the space between my TableView's main header and a section header.

   let tableView: UITableView = {
       let tableView = UITableView(frame: .zero, style: .plain)
       return tableView
   }()

   tableView.tableHeaderView = postView

The post view is dynamic so I cannot set the height for that headerView in the delegate method.

I want the postView and the section below it together, but there is the weird space.

enter image description here

CodePudding user response:

Try this code Setting tableHeaderView height dynamically

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if let headerView = tableView.tableHeaderView {

        let height = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
        var headerFrame = headerView.frame

        //Comparison necessary to avoid infinite loop
        if height != headerFrame.size.height {
            headerFrame.size.height = height
            headerView.frame = headerFrame
            tableView.tableHeaderView = headerView
        }
    }
}

CodePudding user response:

Try the following:

let tableView: UITableView = {
    let tableView = UITableView(frame: .zero, style: .grouped)
    return tableView
}()

tableView.tableHeaderView = postView
  • Related