94

I know how to change the height of the section headers in the table view. But I am unable to find any solution to change the default spacing before the first section.

Right now I have this code:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return 0;
    }
    return 10;
}

enter image description here

2
  • 1
    stackoverflow.com/questions/5441938/… See this link... Commented Jul 17, 2013 at 12:35
  • @JitendraDeore Thanks for guiding me in the right direction Commented Jul 17, 2013 at 12:42

11 Answers 11

224

Return CGFLOAT_MIN instead of 0 for your desired section height.

Returning 0 causes UITableView to use a default value. This is undocumented behavior. If you return a very small number, you effectively get a zero-height header.

Swift 3:

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return CGFloat.leastNormalMagnitude
        }
        return tableView.sectionHeaderHeight
    }

Swift:

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
        return CGFloat.min
    }
    return tableView.sectionHeaderHeight
}

Obj-C:

    - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return CGFLOAT_MIN;
    return tableView.sectionHeaderHeight;
}
Sign up to request clarification or add additional context in comments.

9 Comments

In Swift,'CGFLOAT_MIN' is unavailable: use CGFloat.min instead.
CGFloat.min caused a crash, because CGFloat.min returns negative value like -0.0000000000001
Perhaps this is pedantry, but CGFloat.min is not a very small number, it's a very large negative number. If you wanted a very small number you would use epsilon.
In Swift 3 it’s CGFloat.leastNormalMagnitude
Advise: Dont use this value on estimatedHeightForHeaderInSection, the app will crash.
|
31

If you use tableView style grouped, tableView automatically set top and bottom insets. To avoid them and avoid internal insets setting, use delegate methods for header and footer. Never return 0.0 but CGFLOAT_MIN.

Objective-C

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    // Removes extra padding in Grouped style
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    // Removes extra padding in Grouped style
    return CGFLOAT_MIN;
}

Swift

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    // Removes extra padding in Grouped style
    return CGFloat.leastNormalMagnitude
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    // Removes extra padding in Grouped style
    return CGFloat.leastNormalMagnitude
}

1 Comment

I additionally had to return nil for viewForHeaderInSection for the header to fully disappear.
28

This worked for me with Swift 4. Modify your UITableView e.g. in viewDidLoad:

// Remove space between sections.
tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0

// Remove space at top and bottom of tableView.
tableView.tableHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))
tableView.tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))

1 Comment

Lifesaver, thanks for taking the time to mention it here :)
19

It appears that I can't set a table header view with height of 0. I ended up doing the following:

- (void)viewWillAppear:(BOOL)animated{
    CGRect frame = self.tableView.tableHeaderView.frame;
    frame.size.height = 1;
    UIView *headerView = [[UIView alloc] initWithFrame:frame];
    [self.tableView setTableHeaderView:headerView];
}

1 Comment

This would be better to set height at here: - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 1.0f; }
13

You can try this:

In the loadView

_tableView.sectionHeaderHeight = 0;

Then

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

It should be removed as long as you do not have any objects in the header...

And if you want some size of the sectionheader, then change only the return value.

same if you do not get the sectionfooter removed.

_tableView.sectionFooterHeight = 0;

and

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

Well, this works for my problems with the tableview in the iOS7.

Comments

4

You should remove the code self.tableView.tableHeaderView = [UIView new]; after you add

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

1 Comment

It is footer height which is causing issue in my case. Thanks for help.
3

In Swift 4

Remove extra top padding in grouped tableview.

Here height is given 1 as minimal height for the section header because you cannot give 0 as tableview will take it default top margin if assigned zero height.

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 1
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return UIView()
}

Comments

2

you can use viewForHeaderInSection and return a view with any height.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    int height = 30 //you can change the height 
    if(section==0)
    {
       UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];

       return view;
    }
}

2 Comments

I'm not asking about section headers, but the table header.
then you can directly pass A uiview to the table header view.
2

In swift 2.0

func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {

        return yourHeight
    }

Comments

2

These two lines are enough.

tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0
    

For me, it is working in Xcode 12.2, iOS 14.2

Comments

0

Example of viewForHeaderInSection:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 118)];
view.backgroundColor = COLOR_DEFAULT;

NSString* key = [self.tableKeys objectAtIndex:section];
NSArray *result = (NSArray*)[self.filteredTableData objectForKey:key];
SZTicketsResult *ticketResult = [result objectAtIndex:0];

UIView *smallColoredView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 3)];
smallColoredView.backgroundColor = COLOR_DEFAULT_KOSTKY;
[view addSubview:smallColoredView];

UIView *topBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 8, 320, 40)];
topBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
[view addSubview:topBackgroundView];

UILabel *totalWinnings = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 300, 40)];
totalWinnings.text = ticketResult.message;
totalWinnings.minimumFontSize = 10.0f;
totalWinnings.numberOfLines = 0;
totalWinnings.backgroundColor = [UIColor clearColor];
totalWinnings.font = [UIFont boldSystemFontOfSize:15.0f];
[view addSubview:totalWinnings];

UIView *bottomBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 320, 58)];
bottomBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
[view addSubview:bottomBackgroundView];

UILabel *numberOfDraw = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 290, 58)];
numberOfDraw.text = [NSString stringWithFormat:@"sometext %@",[ticketResult.title lowercaseString]];;
numberOfDraw.minimumFontSize = 10.0f;
numberOfDraw.numberOfLines = 0;
numberOfDraw.backgroundColor = [UIColor clearColor];
numberOfDraw.font = [UIFont boldSystemFontOfSize:15.0f];
[view addSubview:numberOfDraw];

return view;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.