Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UITableView Hide Header Space

In a view controller I have only a UITableView. In IB I have made Header & Footer height as 1, and have also added the following code but yet above the 1st cell their is lots of space of header. I want to get rid of that space. Even the scrollbar starts from the 1st cell and not on top.

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
CGFloat height = 0.0001;
return height;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
CGFloat height = 0.0001;
return height;
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// Create Empty View
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width,
                                                        [self tableView:self.visitorlistsTv heightForHeaderInSection:section]) ];
return view;
}

-(UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
// Create Empty View
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width,
                                                        [self tableView:self.visitorlistsTv heightForFooterInSection:section]) ];
return view;

}

With the above code, the footer part is hidden. But can't get the header hidden too.

Looking at other links for solution I have also added TableView in a View, have added constraints to the tableview, but still the header part is still their.

Where am I going wrong ? How to get rid of it ?

like image 746
Tvd Avatar asked Aug 05 '14 15:08

Tvd


2 Answers

If you're trying to remove the space above your UITableView, try changing it's contentInset property instead of making a custom header with small height:

self.tableView.contentInset = UIEdgeInsetsMake(-20.0f, 0.0f, 0.0f, 0.0f);

You may want to tweak the first number to adjust it to your specific view scene.

Understand that this is also a workaround (source), but it's more clean method than creating a custom table view header.

like image 64
Rinat Khanov Avatar answered Sep 26 '22 00:09

Rinat Khanov


I have another solution for this (checked on iOS 10), for me the accepted solution didn't work.

In my case, I had variable height section headers, so returning 0 height for first section was not acceptable. I used grouped table style because I needed the separators to frame the section headers too.

Solution: be sure you don't have something like this anywhere in your code:

self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

I used this to remove the separators for the empty rows, but with grouped style that is not necessary.

I know this won't be the case for all of you, but it might help for those in my situation.

like image 42
Calin Drule Avatar answered Sep 27 '22 00:09

Calin Drule