Remove empty space before cells in UITableView
Asked Answered
L

32

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?

Lysol answered 20/9, 2013 at 0:6 Comment(0)
M
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"
Matteo answered 30/10, 2013 at 8:11 Comment(6)
Deselecting "Under Top Bars" on the UIView worked just fine for me. Thanks.Key
I was going mad using "Paging Enabled" and the scroll only shifting by the navigation bar height instead of the whole cell. Thanks.Phyla
The Third option: deselect "Adjust scroll view insets" - worked like a charm. thanks lekksi!Geosynclinal
self.automaticallyAdjustsScrollViewInsets = false in Swift working "de pelos"!Stroke
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.Sapp
scrollIndicatorInsets also needs set to zero same way otherwise scroll indicators keep getting smaller.Sapp
F
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.

Fraction answered 11/10, 2013 at 16:14 Comment(8)
this is really really strange. This solved the issue. Any idea when this happens?Countertype
I think you can solve it a lot easier by following my answer from Oct 10'13Plan
Had same issue. Removing it and readding worked for me also.Townshend
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.Matteo
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.Sloshy
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.Inane
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 toOutlaw
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 spaceInitiatory
P
49

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.

Plan answered 10/10, 2013 at 12:58 Comment(3)
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 problemWasp
This solved the problem but now header sections behave differently. Anyone?Bost
Bless you. Wherever you are in this world. Bless you forever.Greig
P
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.

Pentylenetetrazol answered 16/10, 2016 at 12:38 Comment(1)
still works on iOS 15. Thanks mate. cheersGutsy
L
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

Leatherback answered 4/8, 2014 at 15:20 Comment(1)
I have a grouped UITableView and unchecking "Adjust Scroll View Insets" worked.Shirashirah
I
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://mcmap.net/q/65826/-39-automaticallyadjustsscrollviewinsets-39-was-deprecated-in-ios-11-0

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

Hope this helps.

Iritis answered 27/11, 2017 at 17:57 Comment(0)
U
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;
}
Udale answered 27/4, 2014 at 16:27 Comment(1)
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?Vienne
M
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)
Madra answered 2/8, 2019 at 9:38 Comment(0)
G
4

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

enter image description here

Grapnel answered 16/8, 2018 at 9:22 Comment(0)
O
4

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.

Orose answered 7/3, 2019 at 6:17 Comment(0)
V
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

V2 answered 20/9, 2013 at 4:15 Comment(0)
L
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.

Lysol answered 21/9, 2013 at 14:11 Comment(0)
M
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
}
Majestic answered 22/6, 2017 at 22:29 Comment(1)
Thank you so much. i was trying this whole day. finally got the correct Answer.Cabriolet
T
2

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

Turney answered 27/4, 2016 at 0:23 Comment(0)
K
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.

Komarek answered 16/8, 2017 at 19:22 Comment(1)
That did it for me as well. Thanks!Vento
B
2

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.

Bemuse answered 26/5, 2023 at 1:35 Comment(0)
E
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

Evulsion answered 30/3, 2014 at 5:33 Comment(0)
D
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).

Dishonesty answered 19/10, 2014 at 15:30 Comment(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! :)Isopleth
G
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!

Gibeon answered 14/8, 2017 at 13:35 Comment(0)
P
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"
Parthinia answered 22/11, 2013 at 20:13 Comment(0)
A
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];
Alliterate answered 25/11, 2013 at 12:31 Comment(0)
J
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.

Jampack answered 22/5, 2014 at 11:32 Comment(0)
C
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.

Complex answered 4/10, 2014 at 12:44 Comment(0)
K
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.

Knucklebone answered 14/5, 2015 at 19:24 Comment(0)
C
0

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

Carrack answered 29/7, 2016 at 5:52 Comment(0)
S
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.

Seabrooke answered 7/11, 2016 at 15:32 Comment(0)
E
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

Expressivity answered 13/9, 2017 at 9:48 Comment(0)
J
0

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

Justificatory answered 1/4, 2019 at 12:23 Comment(0)
I
0

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

self.extendedLayoutIncludesOpaqueBars = true
Insufferable answered 7/5, 2019 at 21:46 Comment(0)
P
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))
Pileum answered 23/8, 2021 at 11:28 Comment(0)
S
-1

Set the content insets to zero:

Schreib answered 9/2, 2018 at 6:40 Comment(0)
K
-2

check for

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}
  • mention tableview delegate function cellForRowAtIndexPath:
  • it can handled both ios 6 and 7.
Katricekatrina answered 12/8, 2014 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.