9

Please note, I'm going to refer to points as pixels in this question.

I have a grouped UITableView with 3 sections, each with a 40 pixel tall header view. The first header in the table view seems to be given a y position of 35 pixels by the system.

I've tried messing around with automaticallyAdjustsScrollViewInsets and a few other iOS 7 automatic pieces, but to no avail.

Why is my UITableView content being inset by 35 pixels?

EDIT: I've seen this answer and many other threads on this. I have valid headers and header heights. Also, setting the default to FLT_MIN, 0.01f, 1.0f or 100.0f doesn't fix the problem.

Here is my header implementation:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    UIView *header = (self.headerViews.count > section ? self.headerViews[section] : nil);
    return (header.viewHeight ? : FLT_MIN);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = (self.headerViews.count > section ? self.headerViews[section] : nil);
    return (header.viewHeight ? header : nil);
}

I'm also setting:

- (BOOL)automaticallyAdjustsScrollViewInsets{return NO;}

and

[self setSectionHeaderHeight:FLT_MIN];
Community
  • 1
  • 1
RileyE
  • 10,874
  • 13
  • 63
  • 106
  • why are you referring to points as pixels? just say "points" if you mean "points". there is a reason for this distinction. – Michael May 14 '14 at 19:59
  • Have you looked into this [question](http://stackoverflow.com/a/18938763/2150138)? – Callistino May 14 '14 at 19:59
  • @Michael Because pixels are more widely understood and searched for. – RileyE May 14 '14 at 19:59
  • hmm, someone who doesn't understand the difference shouldn't try to answer the question anyhow, IMHO – Michael May 14 '14 at 20:00
  • @Callistino Yes, I have. The biggest difference between that answer/question and mine is that I have headers with a height, so `0.0f` isn't even a factor. Also, I've tried setting it for the sake of testing. – RileyE May 14 '14 at 20:02
  • 1
    Have you also used heightForHeaderInSection to set your appropriate header height? – Jordan Clifton May 14 '14 at 20:04
  • @JordanClifton Yes, I have. – RileyE May 14 '14 at 20:08
  • There are many answers to that question. I remember having the same problem in the past and only one of them worked. So make sure to try all of [them](http://stackoverflow.com/questions/18880341/why-is-there-extra-padding-at-the-top-of-my-uitableview-with-style-uitableviewst/18938763#18938763). Also, are you using a `UITableViewController` or a `UITableView` inside a `VC`? – Callistino May 14 '14 at 20:12
  • @Callistino It's a `UITableView` inside a `UIViewController` – RileyE May 14 '14 at 20:16
  • The "only way" I know of to get a **35 points y offset** in that combination, is if you are also **embedded** on a `NavigationController` and you `VC`'s top constraint is set to the bottom of the `NavC`. – Callistino May 14 '14 at 20:21
  • @Callistino "top constraint"? I feel like you're on to something, as this is only affecting **some** of my tables. – RileyE May 14 '14 at 20:22
  • You must have a **top constraint** on your `TV` to your `VC's main view`. That is the one I am referring to. – Callistino May 14 '14 at 20:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52706/discussion-between-rileye-and-callistino) – RileyE May 14 '14 at 20:35

3 Answers3

8

It seems that this:

[self setTableHeaderView:[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width, FLT_MIN)]];

In init, where self is a UITableView, will get rid of the top 35 points.

RileyE
  • 10,874
  • 13
  • 63
  • 106
  • I can see how this is working and none of the other did. Again, your case is specific to a `UITableView` inside a `VC` so all the other solutions involving `contentInset` or `automaticallyAdjustsScrollViewInsets` would not work since they would be targeting the `VC` when the issue was inside the `UITableViewController` itself. – Callistino May 14 '14 at 20:46
  • This is gold. Not that there's a difference between a table's section header view, which this isn't. This is a **table header**. For some reason just one of my tables had this set to a non zero value after a lot of fiddling in Interface Builder, I am guessing some IB bug. I wish I had seen this earlier. Thanks. – Eran Goldin Apr 19 '15 at 18:33
3

Yep,i get this problem when i set tableview.tableFooterView and custom set section header height, then first section height has over 35 points.

Then i add a

tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))

worked for me.

Kingiol
  • 1,145
  • 9
  • 9
2

There are multiple ways of getting the same issue you are getting and therefore, multiple solutions. I will try to list the ones I know with their corresponding solutions. Most of them are copied from this question.

UITableView inside a UIViewController

This is a common one that trips people off because they think the problem is related to their UITableView and most of the time is actually the parent UIViewController.

If your VC is embedded on a NavigationController. You will get a 35 points y offset as mentioned here.

Solutions

  • In Xcode Version > 5 on VC untick Extended Edges "Under Top Bars under the Attributes Inspector to remove the top UITableView content inset.

  • Constraints: Your VC has a main view where all the other subviews are laid including your UITableView. You need to make sure that all constraints from your UITableView are explicitily set and satisfied/non-ambiguous.

  • Set self.navigationController.navigationBar.translucent = YES;

  • Set self.automaticallyAdjustsScrollViewInsets = NO;. This one can also be set on the storyboard by unchecking the Adjust Scroll View Insets checkbox for the view controller layout.

  • Set self.edgesForExtendedLayout = UIRectEdgeNone;


General UITableView/UITableViewController

  • Set the tableView.separatorInset = UIEdgeInsetsZero;

  • Set the tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0); Pay attention to the -35. The negative number offsets the view.

  • Declare this method (Can also be used for FooterSection):

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
Community
  • 1
  • 1
Callistino
  • 1,075
  • 11
  • 14
  • I need to set the height of my headers to `40.0f` and I don't have a need for an inset of `-20`, either. Also, I'm not using XIBs or auto-layout. As for the rest, that's already all set, but I still have a `35.0f` point inset. However, this is a good amalgamation of all of the information from other answers. – RileyE May 14 '14 at 20:47
  • Yes, I didn't update the `-20` offset when copying here. – Callistino May 14 '14 at 21:17