178

I am currently trying to put a UITableView in a different location rather than at the top of my view controller. With this said, it is trying to add the header in the top to account for the navigation bar, but this is not needed since I do not have at the top of my controller.

If I put the top left corner of the UITableView where I want the cells to be, it doesn't put the cells there:

Now, if I just move the table view up so the cells are in the correct place, I get a different problem - the cells can be moved up to there when scrolling (this is with my finger above the navigation controller):

When I let go, yes - the cells will go right below the search bar, but this is obviously a problem how you can bring them above it.

How would I go about doing this? Is there any easier way?

Julian
  • 9,299
  • 5
  • 48
  • 65
hetelek
  • 3,776
  • 5
  • 35
  • 56

32 Answers32

379

Do the cells of the UITableView show on the empty space when you scroll down?

If so, then the problem might be the inset that is added to the UITableView because of the Navigation controller you have in your view. The inset is added to the table view in order for the content to be placed below the navigation bar when no scrolling has occurred. When the table is scrolled, the content scrolls and shows under a transparent navigation bar. This behavior is of course wanted only if the table view starts directly under the navigation bar, which is not the case here.

Another thing to note is that iOS adjusts the content inset only for the first view in the view hierarchy if it is UIScrollView or it's descendant (e.g. UITableView and UICollectionView). If your view hierarchy includes multiple scroll views, automaticallyAdjustsScrollViewInsets will make adjustments only to the first one.

Here's how to change this behavior:

a) Interface Builder

  • Select the view controller
  • Open Attributes inspector
  • There's a property called "Adjust scroll view insets" in IB's attribute inspector (when a view controller is selected) which is on by default. Uncheck this option:


    (Image courtesy of Dheeraj D)

I'm not sure which Xcode version introduced this option (didn't spot it in the release notes), but it's at least available in version 5.1.1.

Edit: To avoid confusion, this was the third option mentioned in the comments

b) Programmatically

Add this to i.e. viewDidLoad (credits to Slavco Petkovski's answer and Cris R's comment)

// Objective-C
self.automaticallyAdjustsScrollViewInsets = NO;

// Swift
self.automaticallyAdjustsScrollViewInsets = false

c) This might be relevant for old schoolers

You can either fix this by adding

tableView.contentInset = UIEdgeInsetsZero

//Swift 3 Change
tableView.contentInset = UIEdgeInsets.zero

Or if you are using IB and if the navigation bar is not transparent (can't tell from the screenshot)

  • Select the view controller
  • Open Attributes inspector
  • In View Controller options Extend Edges section deselect "Under Top Bars"
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
lekksi
  • 4,868
  • 1
  • 16
  • 13
  • 2
    Deselecting "Under Top Bars" on the UIView worked just fine for me. Thanks. – Andreas Øverland Jan 22 '14 at 08:20
  • I was going mad using "Paging Enabled" and the scroll only shifting by the navigation bar height instead of the whole cell. Thanks. – LordParsley Apr 22 '14 at 20:38
  • 8
    The Third option: deselect "Adjust scroll view insets" - worked like a charm. thanks lekksi! – Stas Zhukovskiy May 20 '14 at 21:22
  • 2
    self.automaticallyAdjustsScrollViewInsets = false in Swift working "de pelos"! – Cris R Feb 10 '15 at 20:48
  • 1
    Thanks setting UIEdgeInsetsZero is required if reusing a table in a new table controller every time its shown otherwise the same white gap appears and gets bigger and bigger. – malhal Jan 11 '16 at 21:55
  • scrollIndicatorInsets also needs set to zero same way otherwise scroll indicators keep getting smaller. – malhal Jan 12 '16 at 11:38
72

I had this same problem and I'm pretty sure I had a UIView in there at one point. Simply copying the entire table view, deleting it, and pasting it again fixed it for me.

irblue
  • 841
  • 7
  • 8
  • 7
    this is really really strange. This solved the issue. Any idea when this happens? – MiQUEL Oct 27 '13 at 01:17
  • I think you can solve it a lot easier by following my answer from Oct 10'13 – netshark1000 Jan 21 '14 at 15:35
  • Had same issue. Removing it and readding worked for me also. – sma May 22 '14 at 19:34
  • 3
    As far as I know, scroll view content insets are adjusted automatically (if automaticallyAdjustsScrollViewInsets = YES, which it by default is) only if the scroll view is the first child in the view controller view hierarchy. My guess is that this solution works because after copy-pasting the table view it's no longer the first view in the view hierarchy. – lekksi Sep 11 '14 at 07:44
  • What happens is that when the tableView is the first thing you add to the view, the system thinks that it has to leave room for the navigation. My guess here is that some people add the tableView and then the headerView. After some issues, they then remove the tableView and add it again, what happens then is that the headerView is the first thing on the hierarchy, so the system doesn't have to adapt anymore, problem solved. I know because I only use code, so in my case had to add first the header and then the table to fix it. – Lluis Gerard Jul 14 '15 at 14:13
  • I fixed this by dragging the tableView down in the view Hierarchy so that it was no longer the first item under the parent View. This is why deleting and re-adding the tableView also works as it accomplishes the same thing. – user2096580 Oct 22 '15 at 20:54
  • For me it was an Auto-Layout issue. I basically had to remove the top-layout-constraint (or just reset). And make sure: 1. no padding, 2. where it is aligned to – skofgar Nov 05 '15 at 03:29
  • This also worked for me. If you look at the original question you can see in the "Prototype Cell" theres a lot of space above it to the top of the table view. There is supposed to be no additional space above where it says "Prototype Cell". My project was doing something similar (it was an artifact of when I copied my tableView over initially from another VC). Recopying and pasting my UITV removed that extra space – wyu Sep 01 '16 at 20:55
48

Select the tableview in your storyboard and ensure that the style is set to "Plain", instead of "Grouped". You can find this setting in the attributes Inspector tab.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
netshark1000
  • 7,245
  • 9
  • 59
  • 116
  • amazing.. this was the solution for me weirdly.. I just copied and pasted the whole view controller but somehow this pasted view controller's tableView had that weird offset. This answer solved my problem – daisura99 Jul 27 '18 at 13:10
  • This solved the problem but now header sections behave differently. Anyone? – Bruno Muniz Sep 18 '19 at 01:23
  • 1
    Bless you. Wherever you are in this world. Bless you forever. – gabuchan Jul 14 '20 at 19:35
10

NONE of the above helped me. but doing this did help:

self.tableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0)

instead of -64 you can put any other number depending on the height of your navigation bar.

S1LENT WARRIOR
  • 11,704
  • 4
  • 46
  • 60
Tung Fam
  • 7,899
  • 4
  • 56
  • 63
9

In My Case I had a UILabel under the UITableView in view hierarchy.

I moved it "forward" and the blank space appeared. Not sure why but it works like this, if theres anything under the tableView, it hides the blank space.

Also you can try checking/uncheking "Adjust Scroll View Insets" on your view controller inspector on storyboard.

enter image description here

Andre Cytryn
  • 2,506
  • 4
  • 28
  • 43
7

If you are doing this on iOS 11, automaticallyAdjustsScrollViewInsets won't work as it is deprecated, the following code worked for me as per the answer in this post: https://stackoverflow.com/a/44391269/5476481

        if #available(iOS 11.0, *) {
            tblView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }

Hope this helps.

Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75
5

There is may be pre-added UIRefreshControl. I have this problem with code like this:

self.refreshControl = [[UIRefreshControl alloc] init];{
    [self.refreshControl setAttributedTitle:[[NSAttributedString alloc]
                                             initWithString:@"Your string"]];
    [self.refreshControl addTarget:self
                            action:@selector(updateScreen)
                  forControlEvents:UIControlEventValueChanged];

}

As you see, I set self.refreshController immediately after init

But, if you set refreshControl after setup, then top-space don't interfere you.

UIRefreshControl *refreshControl  = [[UIRefreshControl alloc] init];{
    [self.refreshControl setAttributedTitle:[[NSAttributedString alloc]
                                             initWithString:@"Your String"]];
    [self.refreshControl addTarget:self
                            action:@selector(updateScreen)
                  forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refreshControl;
}
akaDuality
  • 384
  • 5
  • 6
  • I can't believe this was the actual problem. It's only an issue for me on <= 9.3.5. How in the world did you figure this out? – Allison Jul 31 '18 at 22:30
5

Swift 5

Try with remove tableHeaderView and tableFooterView

var frame = CGRect.zero
frame.size.height = .leastNormalMagnitude
tblView.tableHeaderView = UIView(frame: frame)
tblView.tableFooterView = UIView(frame: frame)
Community
  • 1
  • 1
Vivek
  • 4,916
  • 35
  • 40
4

In the Storyboard , set the contentInsets of your tableview to Never

enter image description here

Marwen Doukh
  • 1,946
  • 17
  • 26
3

I can't say for sure but it looks like you've got an extra UIView stuck between where the Prototype Cells begin and the top of the UITableView. My image looks exactly like yours if you remove the text/lines I added in.

enter image description here

Mike S
  • 11,329
  • 6
  • 41
  • 76
3

I'm still not sure what caused that extra space at the top, but I found that deleting the UITableView with the space and replacing it with a new one took the space away.

I must have dragged a UIView in there accidentally, but I couldn't select it so I couldn't delete.

hetelek
  • 3,776
  • 5
  • 35
  • 56
3

In my application also I've experienced an issue like this. I've set top edge inset of tableView zero. Also tried set false for automaticallyAdjustsScrollViewInsets. But didn't work.

Finally I got a working solution. I don't know is this the correct way. But implementing heightForHeaderInSection delegate method worked for me. You have to return a non-zero value to work this (return zero will display same space as before).

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    return 0.1
}
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
3

In iOS 11 and above, apple has changed property to adjust content inset. Use contentInsetAdjustmentBehavior and set .never to remove extra space above UICollectionView, UIScrollView, UITableView.

if #available(iOS 11.0, *) {

    // For scroll view
    self.scrollView.contentInsetAdjustmentBehavior = .never

    // For collection view
    self.collectionView.contentInsetAdjustmentBehavior = .never

    // For table view
    self.tableView.contentInsetAdjustmentBehavior = .never

} else {
    self.automaticallyAdjustsScrollViewInsets = false
}

I hope this will help you.

Mayur Karmur
  • 2,119
  • 14
  • 35
2

Check your tableview frame in storyboard or xib. Mine by default has a y position value, hence the bug

Arthi
  • 128
  • 1
  • 7
2

In 2017, what is working for me is simply this one line

navigationController?.navigationBar.isTranslucent = false

I have no idea why that would even affect it. Perhaps someone could explain. But I'm pretty sure this is the answer most people would look for.

Dew Time
  • 818
  • 9
  • 10
1

Well for me the problem occurred when I dragged a prototype cell from the menu into the table view. So I deleted that and just set prototype cell to 1 in table view inspector properties

Junaid Ahmed
  • 175
  • 1
  • 9
1

As long as UITableView is not the first subview of the view controller, the empty space will not appear.

Solution: Add a hidden/clear view object (views, labels, button, etc.) as the first subview of the view controller (at the same level as the UITableView but before it).

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
jr.ong
  • 11
  • 1
  • I spent so much time trying to figure this out, trying many of the suggestions here and on other posts. This is the one that solved it. Thank you! Can you explain why though or post a link to some doc that does? I'm just learning iOS dev and it is so different than Android! :) – Mark Jan 22 '17 at 00:09
1

I am doing this programmatically, and I simply moved my addSubview(tableView) to the bottom after adding all of my other subviews, and it solved my problem!

Not sure why this only appears on device and not my simulator, but either way at least there is a simple solution!

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Austin Whitelaw
  • 107
  • 1
  • 11
1

For those who are using iOS 15 or greater: 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
0

In my case I was using a container view.(Main View --> Contaner View --> UITableView)

The extra space at the top, I think, is like space of the device notification bar (where the time is displayed, the battery). I really do, it's not an assumption.

What I did was from the UI Builder:

  • select the Table View Controller complete
  • Open Attributes inspector
  • Go to -> View Controller -> Layout -> "Select Wants Full Screen"
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

You can use this code into viewDidLoad or viewDidAppear where your table being created:

// Remove blank space on header of table view
 videoListUITableView.contentInset = UIEdgeInsetsZero;

// The iOS device = iPhone or iPod Touch
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;

// Set the height of the table on the basis of number of rows
videoListUITableView.frame = CGRectMake(videoListUITableView.frame.origin.x, videoListUITableView.frame.origin.y, videoListUITableView.frame.size.width, iOSDeviceScreenSize.height-100);



// Hide those cell which doesn't contain any kind of data
self.videoListUITableView.tableFooterView = [[UIView alloc] init];
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51
0

UIView can be inserted at the top and bottom of the table(drag and drop). Set their properties as transparent and height of 1 px. This is to remove the extra padding in front of the cells.

Alexander
  • 9,074
  • 2
  • 15
  • 13
0

I also had this problem the tableView wasn't the last view in the hierarchy. In my case i had a view above (even though they were not overlapping each other), so i dragged it above the tableView in the hierarchy and that made it.

Sebyddd
  • 4,305
  • 2
  • 39
  • 43
0

I just found a solution for this.

Just select tableview and clic Editor -> Arrange -> Send to Front

It worked for me and hope it helps you all.

0

In my case, i was using ContainerViewController and putting UITableViewController in it. After removing ContainerViewController. Issues goes away.

Sandeep Ahuja
  • 931
  • 6
  • 17
0

Xcode 8 bug…

Try this first if your Storyboard UITableView style is set to Plain.

Set the table's style to Grouped and then back to Plain. This removed the space in my app at the head of my UITableView.

Carl
  • 2,896
  • 2
  • 32
  • 50
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/vRNfV.png

0

set ViewController's Extended Edges in storyboard so that underTopBar property is unchecked.

JAHelia
  • 6,934
  • 17
  • 74
  • 134
0

Was having the same issue. I solved it by adding the following to viewDidLoad.

self.extendedLayoutIncludesOpaqueBars = true
GIJoeCodes
  • 1,680
  • 1
  • 25
  • 28
0

Answer up to date with: XCode 12.5.1; IOS 14

The default behaviour of the grouped table views is to have that kind of empty header at the beginning.

The general recommendation, if you don't want to have that space, is to use a .plain table view instead.

If for some reason you can't, then you have to set the header to a view with a height that is not zero, like in the following example:

self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0,
                                                      width: self.tableView.bounds.size.width,
                                                      height: .leastNonzeroMagnitude))
Alessandro Francucci
  • 1,528
  • 17
  • 25
-1

Set the content insets to zero:

Pang
  • 9,564
  • 146
  • 81
  • 122
-2

check for

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}
  • mention tableview delegate function cellForRowAtIndexPath:
  • it can handled both ios 6 and 7.
Andre Cytryn
  • 2,506
  • 4
  • 28
  • 43
Manikandan
  • 253
  • 3
  • 7