Remove empty space before cells in UITableView

IosObjective CUitableviewIos7

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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"

Solution 2 - Ios

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.

Solution 3 - Ios

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.

Solution 4 - Ios

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.

Solution 5 - Ios

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

Solution 6 - Ios

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.

Solution 7 - Ios

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;
}

Solution 8 - Ios

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)

Solution 9 - Ios

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

enter image description here

Solution 10 - Ios

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

Solution 11 - Ios

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.

Solution 12 - Ios

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
}

Solution 13 - Ios

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.

Solution 14 - Ios

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

Solution 15 - Ios

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.

Solution 16 - Ios

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

Solution 17 - Ios

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).

Solution 18 - Ios

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!

Solution 19 - Ios

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"

Solution 20 - Ios

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];

Solution 21 - Ios

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.

Solution 22 - Ios

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.

Solution 23 - Ios

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.

Solution 24 - Ios

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

Solution 25 - Ios

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.

Solution 26 - Ios

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

Solution 27 - Ios

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

Solution 28 - Ios

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

self.extendedLayoutIncludesOpaqueBars = true

Solution 29 - Ios

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))

Solution 30 - Ios

Set the content insets to zero:

Solution 31 - Ios

check for

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}
  • mention tableview delegate function cellForRowAtIndexPath:
  • it can handled both ios 6 and 7.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhetelekView Question on Stackoverflow
Solution 1 - IoslekksiView Answer on Stackoverflow
Solution 2 - IosirblueView Answer on Stackoverflow
Solution 3 - Iosnetshark1000View Answer on Stackoverflow
Solution 4 - IosTung FamView Answer on Stackoverflow
Solution 5 - IosAndre CytrynView Answer on Stackoverflow
Solution 6 - IosJacobo KoenigView Answer on Stackoverflow
Solution 7 - IosakaDualityView Answer on Stackoverflow
Solution 8 - IosVivekView Answer on Stackoverflow
Solution 9 - IosMarwen DoukhView Answer on Stackoverflow
Solution 10 - IosMike SView Answer on Stackoverflow
Solution 11 - IoshetelekView Answer on Stackoverflow
Solution 12 - IosAnusha KottiyalView Answer on Stackoverflow
Solution 13 - IosMayur KarmurView Answer on Stackoverflow
Solution 14 - IosArthiView Answer on Stackoverflow
Solution 15 - IosDew TimeView Answer on Stackoverflow
Solution 16 - IosJunaid AhmedView Answer on Stackoverflow
Solution 17 - Iosjr.ongView Answer on Stackoverflow
Solution 18 - IosAustin WhitelawView Answer on Stackoverflow
Solution 19 - IosPatricio DiegoView Answer on Stackoverflow
Solution 20 - IosVinod JoshiView Answer on Stackoverflow
Solution 21 - IosAlexanderView Answer on Stackoverflow
Solution 22 - IosSebydddView Answer on Stackoverflow
Solution 23 - IosDaniel MezaView Answer on Stackoverflow
Solution 24 - IosSandeep AhujaView Answer on Stackoverflow
Solution 25 - IosCarlView Answer on Stackoverflow
Solution 26 - IosNitinLandgeView Answer on Stackoverflow
Solution 27 - IosJAHeliaView Answer on Stackoverflow
Solution 28 - IosGIJoeCodesView Answer on Stackoverflow
Solution 29 - IosAlessandro FrancucciView Answer on Stackoverflow
Solution 30 - Iossriram neelamegamView Answer on Stackoverflow
Solution 31 - IosManikandanView Answer on Stackoverflow