Fry
Fry

Reputation: 6275

iOS 7 UITableView: How to remove space between navigation bar and first cell

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 here enter 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?

Upvotes: 60

Views: 43060

Answers (16)

Marik ZMR
Marik ZMR

Reputation: 21

I was able to remove the gap between navigation bar and first cell writing this line of code in viewDidLoad()

self.tableView.contentInsetAdjustmentBehavior = .never

I tired this with Static Table Views (Style: Grouped).

Upvotes: 1

Daniel Beltrami
Daniel Beltrami

Reputation: 776

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

Upvotes: 3

iuricernov
iuricernov

Reputation: 106

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

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

Upvotes: 0

Adam Johns
Adam Johns

Reputation: 36373

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).

Upvotes: 0

Ashfaque
Ashfaque

Reputation: 1284

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;

Upvotes: 1

刘志余
刘志余

Reputation: 1

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;
}

Upvotes: -2

Venu Gopal Tewari
Venu Gopal Tewari

Reputation: 5876

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)];

Upvotes: 5

Muhammad_Awaab
Muhammad_Awaab

Reputation: 1588

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

Upvotes: 16

love2script12
love2script12

Reputation: 210

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'

Upvotes: 1

vinylDeveloper
vinylDeveloper

Reputation: 707

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.

Upvotes: 0

Luke
Luke

Reputation: 658

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.

Upvotes: 0

Slavcho
Slavcho

Reputation: 2812

Just add this in you ViewDidLoad method

self.automaticallyAdjustsScrollViewInsets = NO;

Upvotes: 132

Giorgos Ath
Giorgos Ath

Reputation: 1405

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.

Upvotes: 37

Hot'n'Young
Hot'n'Young

Reputation: 491

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;
}

Upvotes: 2

Chuck Krutsinger
Chuck Krutsinger

Reputation: 2930

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.

Upvotes: 0

clmntcrl
clmntcrl

Reputation: 273

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];

Upvotes: 12

Related Questions