6

My Application has a navigation and UITableView. I set backgroundColor to my tableView. Look at the picture bellow. I don't understand why the first cell is not started from top of UITableView. When I use View Debbuger in Xcode, TableViewWrapperView is not fulled in UITableView.

I already searched many times. I removed check Adjust Scroll View Insets and set .zero tableview's contentinsets. At last I made New Project and make it again but I can't.

How can I resolve this problem!!

enter image description here

Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31
Lee Dong Kyoo
  • 61
  • 1
  • 4

10 Answers10

10

Simple Solution for this issue

No need to code for this issue. Just follow below simple step

YourStoryboard.storyboard > YourViewController > Attributes inspector > Uncheck - Adjust scroll view insets.

Here I attached screenshot for reference.

enter image description here

If you like go with code I have also solution :)

- (void)viewDidLoad {
      self.automaticallyAdjustsScrollViewInsets = false
}
Community
  • 1
  • 1
Vivek
  • 4,916
  • 35
  • 40
7

This is for the souls like me who tried everything above and might have missed this. I accidentally used tableview style as Grouped instead of Plain.

So set your Tableview style to "Plain" instead of "Grouped" in xib/storyboard or in code as

Objective C

self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

Swift 4,0

self.tableView = UITableView(frame: CGRect.zero, style: .plain)
Dhilip
  • 498
  • 5
  • 18
  • 1
    i got this issue after refactoring, I forgot that i changed the tableview style in the proces. after two hours of checking the diff, debugging and googling I finally came actoss ur answer. The word style trigger i needed. Thnx due and thanx SO – MQoder Mar 27 '19 at 08:08
  • I can feel the pain bro :-) – Dhilip Mar 27 '19 at 14:34
5

The table view tries to adjust itself to accommodate the navigation bar assuming that navigation bar is translucent and it has to display itself behind it.

To resolve this issue in the storyboard/xib file set the view controller property extends edges under top bar to false as shown below.

enter image description here

Windindi
  • 412
  • 4
  • 11
  • When I unselect "under top bars" the navigation bar's background changes color. Any idea how to keep it the same as when it is selected? – spijs Aug 18 '17 at 11:17
  • @spijs This might be because your navigation bar might have a alpha to its background color. You might want a solid color for the background to maintain consistency. – Windindi Aug 22 '17 at 06:05
5

Found it for iOS 11 and above, use:

    self.tableView.contentInsetAdjustmentBehavior = .never
Nitin
  • 51
  • 1
  • 1
2

Open the storyboard, click on the UITableView. In your sizeInspector set the Headerand Footer in section height field to be 1.

Rikh
  • 4,078
  • 3
  • 15
  • 35
0

Open your storyboard and click on the table view controller/view controller that contains the table view. Open the attribute inspector and untick Adjust Scroll View Insets.

Jacob King
  • 6,025
  • 4
  • 27
  • 45
0

I answer my own question.

I don't know what is different to top layout guide and view. But I resolve to set top constraint 0 to view.

Thank you.

enter image description here

Lee Dong Kyoo
  • 61
  • 1
  • 4
0

Write this two line on viewdidload of your viewcontroller, it will fix your issue

self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;
Bucket
  • 449
  • 3
  • 20
0

solution post iOS 11

tableView.contentInsetAdjustmentBehavior = .never
Reeder32
  • 89
  • 4
0

I have try the code below, but it did not work.

self.edgesForExtendedLayout = UIRectEdgeNone;  
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;  
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

My solution:

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0,
                                                              0.0,
                                                              self.tableView.frame.size.width,
                                                              1.0)];
    header.backgroundColor = [UIColor whiteColor];
    
    return header;
}