Search code examples
iosuitableviewios7

Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7


Starting in iOS7, there is additional space at the top of my UITableView's which have a style UITableViewStyleGrouped.

Here is an example:

enter image description here

The tableview starts at the first arrow, there are 35 pixels of unexplained padding, then the green header is a UIView returned by viewForHeaderInSection (where the section is 0).

Can anyone explain where this 35-pixel amount is coming from and how I can get rid of it without switching to UITableViewStylePlain?


Update (Answer):

In iOS 11 and later:

tableView.contentInsetAdjustmentBehavior = .never

Solution

  • I have found the cause of my original bug and created a sample project showcasing it. I believe there is an iOS7 bug.

    As of iOS7, if you create a UITableView with the Grouped style, but do not have a delegate set on first layout, then you set a delegate and call reloadData, there will be a 35px space at the top that will never go away.

    See this project I made showcasing the bug: https://github.com/esilverberg/TableViewDelayedDelegateBug

    Specifically this file: https://github.com/esilverberg/TableViewDelayedDelegateBug/blob/master/TableViewDelayedDelegateBug/ViewController.m

    If line 24 is active,

    [self performSelector:@selector(updateDelegate) withObject:nil afterDelay:0.0];
    

    there will be an extra 35 px space at the top. If line 27 is active and 24 is commented out,

    self.tableView.delegate = self;
    

    no space at the top. It's like the tableView is caching a result somewhere and not redrawing itself after the delegate is set and reloadData is called.