59
votes

Description of the problem: with iOS 7 in grouped UITableView there is a gap between the top of the table view and the first cell.

The strange part is that when I load the content for the first time the table appears to be ok (first image), but when I scroll down a space appears between top and first cell (second image):

enter image description hereenter image description here

With style plain this behavior does't occur but unfortunately I need to use the grouped table view style.

Any ideas about?

15
Have you by chance implemented this protocol? - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section - mmackh
No, because I don't need the title for the section - Fry
We'll it looks like some thing like some datasource/delegate tableView issue. The other one I can possibly think of is - (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section - mmackh
I've tried to return 0 but the issue remains - Fry
Have you added any insets to the tableview/tableview's scrollview? - mmackh

15 Answers

132
votes

Just add this in you ViewDidLoad method

self.automaticallyAdjustsScrollViewInsets = NO;
37
votes

The answer was very funny for me and my team, and worked like a charm

  • In the Interface Builder, Just move the tableview under another view in the view hierarchy.

REASON:

We observed that this happens only for the First View in the View Hierarchy, if this first view is a UITableView. So, all other similar UITableViews do not have this annoying section, except the first. We Tried moving the UITableView out of the first place in the view hierarchy, and everything was working as expected.

16
votes

Go to the attributes inspector of the View Controller by selecting the xib or the controller in Storyboard. Uncheck the Adjust Scroll View Insets in Layout. It will solve the problementer image description here

12
votes

If you are using a UITableView with grouped style and only 1 group, use plain style instead solve your problem.

UITableViewController *tableViewController;
tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
5
votes

Simplest solution to this problem in grouped type tableView is:

tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)];
3
votes

For solve this problem make sure that you unchecked this field on ViewController:

On ViewController >> Attibutes Inspector >> Layout (unchecked - Adjust Scroll View Insets)

enter image description here

if this is not enough, try this on TableView, set Style to Plain:

Example image

This will solve your problem

2
votes

guys! I had the same problem. TableView appeared with free space between nav bar and first cell. This code saved me:

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

There was a space above the UITableView itself, and also between the cells. Found my solution in 2 different answers above..posting it again so that nobody misses both solutions.

This removed the space from above :

self.automaticallyAdjustsScrollViewInsets = NO;

and this removed the spaces below the cells : Setting the 'Style' attribute of the UITableView to 'Plain'

1
votes

I am working with Xcode 7.3.1, iOS9, and swift 2. I would like to share what worked for me:

Just add this in you ViewDidLoad method

self.automaticallyAdjustsScrollViewInsets = false;
0
votes

Starting in iOS 7, you automatically get a group header and some space. Compare your app to the Settings app and you'll see. You can work around it by telling the UITableView to set the header height as small as possible. See How to hide first group header.

0
votes

I realize this was answered a long time ago, but for anyone else running into a similar situation, I'll share what ended up working for me:

If you're using a xib, select the top level view and set 'Status Bar' to 'None' in the Simulated Metrics area of the Attributes Inspector. Fixed my spacing issue right up.

0
votes

For me this didn't work

self.automaticallyAdjustsScrollViewInsets = NO;

This only caused the table view to sit under the NavigationBar.

What worked for me was to go into my storyboard, and resize my tableView. It seems my tableView had a 20px gap at the top to allow for the statusBar. I scaled it and everything lined up perfectly.

0
votes

I was seeing this extra space at the top of one of my table views in a very strange situation.

After I added a UIScrollView as the root view of my first controller in my navigation stack, if I presented the next controller as a Show Detail segue, my next controller would have the space above its table view.

The solution for me was to change the segue to Present Modally and everything went back to normal.

The strangest part of the segue changing was that before I added my root view being a UIScrollView, my Show Detail segue was presenting the next controller modally. Yet after I added the root UIScrollView, the Show Detail segue was pushing on the next controller (which was not what I wanted).

0
votes

Had a similar problem, even setting automaticallyAdjustsScrollViewInsets to NO. This solved for me:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return CGFLOAT_MIN;
}
-2
votes

In the UIViewController.h iOS9 SDK have a method:

@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets NS_AVAILABLE_IOS(7_0); // Defaults to YES

is not a property, so

self.automaticallyAdjustsScrollViewInsets = NO; 

should be

-(BOOL)automaticallyAdjustsScrollViewInsets{   
     return NO;
}