33

I'm trying to position a TableView inside my ViewController view but leaving a 44 height gap between the bottom of the navigation bar and the top of the table. I then wanted to place a UITextField inside that gap to act as a stationary header. For some reason, the TableView has an empty white space above the start of the "Prototype Cells". Its just white space. Here is what it looks like in the storyboard.

storyboard

When viewing the app display, this is what it looks like:

app1

When scrolling the table, it goes all the way up to the correct place:

app2

MattyG
  • 8,449
  • 6
  • 44
  • 48
Fenda
  • 1,385
  • 3
  • 14
  • 24
  • possible duplicate of [Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7](http://stackoverflow.com/questions/18880341/why-is-there-extra-padding-at-the-top-of-my-uitableview-with-style-uitableviewst) – Manab Kumar Mal Sep 29 '15 at 15:35

10 Answers10

91

Try to look in the 'attribute inspector' (in the right menu) of the Participants ViewController. Check for the option 'Extend Edges' and uncheck the 'Under Top Bars', and then relocate your tableview.

Another possible solution is to just uncheck the option 'Adjust Scroll View Insets'. The reason is that when the viewController extends its edges, let's say under the top bar, the tableView's scrollView automatically adjusts its inset top, so that the content of the tableView will start exactly under the top bar. But in your case this is not needed, since your tableView itself starts under the bar.

Focus on the ViewController and got to the Attribute Inspector tab:

enter image description here

oren
  • 3,159
  • 18
  • 33
  • Great! This works well except that the "Enter name" text field is not visible when in landscape mode. Perhaps its hidden below the table because the navigation bar is thinner? – Fenda Jan 07 '15 at 20:24
  • Sounds like just a simple case for constraints. Try to set constraint for the textField - 'Top Space to Top Layout Guide', this should cause the textField to be under the top layout guide by the constant you set, in this case, the topLayoutGuide is exactly the bottom of the navigation bar. – oren Jan 08 '15 at 14:31
  • Just updated my answer with a better solution. by the way, I just want to emphasize that it doesn't related to header view, as the other answer suggested... at least not as I see it.. – oren Jan 08 '15 at 19:06
  • 1
    The second paragraph of your answer works perfectly. Thank you! – Fenda Jan 11 '15 at 15:00
  • 1
    Thanks you sir! This is my first 'V' :) – oren Jan 11 '15 at 16:19
13

Try this one:

self.tableView.contentInset = UIEdgeInsetsMake(-65, 0, 0, 0)
Nati Lara-Diaz
  • 2,056
  • 16
  • 8
9

Just add this in you ViewDidLoad method

self.automaticallyAdjustsScrollViewInsets = NO;
Fadi Abuzant
  • 476
  • 8
  • 13
6

change your table view style from grouped to plain

as shown here

Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
venkatesh
  • 81
  • 1
  • 2
1

You should not need to change the default setting for Extend Edges.

Looks like the real problem is a blank table header view in your storyboard. It's showing in the screenshot you provided of your storyboard, right below the Enter Name view and right above the Prototype Cells view. Delete it.

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
  • Perhaps I am being crazy but how do you delete the blank table header view? I can't click on anything in that blank space. – Fenda Jan 08 '15 at 11:01
  • Maybe it's me. If you expand the Table View in the Structure editor on the left, what do you see? What is in that white space? Can you provide a screenshot? – Mike Taverne Jan 08 '15 at 15:09
  • The fact the space disappears when you scroll the table suggests the white space is part of the table view. The only way I know of is if you have a header view in your table. – Mike Taverne Jan 08 '15 at 15:16
1

My issues is, I set tableHeaderView as new UIView like this

self.tableHeaderView = UIView(frame: .zero)

Remove this line, the issue is gone. Try this:

//self.tableHeaderView = UIView(frame: .zero)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
AshvinGudaliya
  • 3,234
  • 19
  • 37
1

This is the 2022, iOS 15 solution if anyone stumbles upon this.

if #available(iOS 15.0, *) {
    UITableView.appearance().sectionHeaderTopPadding = CGFloat(0)
}
anomaddev
  • 9
  • 2
0

I just found a solution for this. In my case, i was using TabBarViewController, A just uncheck the option 'Adjust Scroll View Insets'. Issues goes away.

https://i.stack.imgur.com/qgQI8.png

0

For those who are using iOS 15 or greater and section headers are being used: this will likely not solve the issue, even though as of May 2023, only solutions like these are the ones popping up in search engines.

iOS 15 introduced UITableView.sectionHeaderTopPadding, which is not 0 by default. So you need:

let tableView = UITableView()
tableView.sectionHeaderTopPadding = 0

This answer on another post is what relieved me from 2 hours of trying to figure this out.

monstermac77
  • 256
  • 3
  • 24
-1

For Objective-C folks, add this in your ViewDidLoad method.

[self.tableView setContentInset:UIEdgeInsetsMake(-20, 0, 0, 0)];