简体   繁体   中英

Gap between table header and first cell

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:

查看层次结构

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM