Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove uitableview header gap

UITableView example of unwanted behavior

pictured above we have an iphone, a custom menu controller, and tableView slightly scrolled down

There is also a header on the table view, which is a grey horizontal bar, with a slightly more grey horizontal bar.

I intended for the header to stick to the very top of the view, but instead there is a gap which the scrollable section of the table view can fill when the user scrolls down, but the header remains in an unsightly position below the top of the scrollable area.

How do I fix this?

I tried setting this to a tiny value

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .001;
}

but this removes the grey background in the header.

Any other suggestions? I also looked at other answers to similar questions but they were not quite addressing my problem (or didn't work).

Also, my tableView and header is made programmatically, no storyboards or xibs or interface builder involved.

edit: guys I took a completely different approach to what I was actually trying to develop, so this problem isn't applicable to me any longer. but self.automaticallyAdjustsScrollViewInsets = NO; was not the solution to that problem as I was already using it

like image 885
CQM Avatar asked Aug 06 '15 21:08

CQM


2 Answers

UITableViewController's views are automatically inset in iOS7 and higher so that they don't start below the navigation bar/status bar. You can controler this behavior by the setAutomaticallyAdjustsScrollViewInsets: method of UIViewController.

like image 75
Roman Kabachenko Avatar answered Oct 16 '22 19:10

Roman Kabachenko


I had something similar happen to me in a Swift project but the Property changed is the same. I found that adding this line

self.automaticallyAdjustsScrollViewInsets = NO;

solved my issue and the gap disappeared.

like image 45
Cole Avatar answered Oct 16 '22 20:10

Cole