2

I have a grouped table view with multiple sections, there is a gap between the header and the first cell which I don't understand. It's not the section header/footer and when I use the debug view hierarchy it looks like the y origin of the cell frame is causing the problem:

View hierarchy

(UITableViewWrapperView highlighted)

The red and blue views are headers and footers, I coloured them so I could rule them out as being the problem.

If I get a printout of the first cell the frame has properties: frame = (0 35; 320 86)

So it looks like the y coordinate of 35 is causing the gap between the cell and the header. If I manually set the y coordinate of the cell to 0 in cellForRowAt it doesn't seem to do anything. I already have automaticallyAdjustScrollViewInsets to false.

A point in the right direction would be much appreciated.

mike
  • 2,073
  • 4
  • 20
  • 31
  • http://stackoverflow.com/questions/19111451/ios-7-uitableview-how-to-remove-space-between-navigation-bar-and-first-cell – Sahil Apr 11 '17 at 15:34
  • @Sahil the majority of the answers in that post mention automaticallyAdjustScrollViewInsets, which I said in the original question has already been tried. Also tried adjusting the tableView contentInset, doesn't work either. – mike Apr 11 '17 at 15:39
  • Also check your Layout Margins in Interface Builder, if they're set to Default you might want to try changing them to Explicit and setting to 0. – Norman Apr 11 '17 at 15:54

1 Answers1

0

Same issue happens to me now.

I just worked around to solve this. Please check this. It may work for you.

tableView.tableHeaderView = myView

I used auto layout and constraints in the header view. After I set it to my tableHeaderView, I got header view frame as (0 35 0 0).

I got the height of my header view using systemLayoutSizeFitting.height and I know the width as 414 and resetted the header view frame to (0 0 414 200) and then I set the header view to my tableHeaderView again.

let height = myView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height //height = 200

myView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: height)
tableView.tableHeaderView = myView

Now it is working for me.