hetelek
hetelek

Reputation: 3886

Remove empty space before cells in UITableView

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?

Upvotes: 178

Views: 121329

Answers (30)

monstermac77
monstermac77

Reputation: 330

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.

Upvotes: 3

Tung Fam
Tung Fam

Reputation: 8147

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.

Upvotes: 10

Alessandro Francucci
Alessandro Francucci

Reputation: 1660

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

Upvotes: 1

Vivek
Vivek

Reputation: 5213

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)

Upvotes: 5

GIJoeCodes
GIJoeCodes

Reputation: 1850

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

self.extendedLayoutIncludesOpaqueBars = true

Upvotes: 0

JAHelia
JAHelia

Reputation: 7902

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

Upvotes: 0

Ashish P
Ashish P

Reputation: 3056

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.

Upvotes: 4

Marwen Doukh
Marwen Doukh

Reputation: 2050

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

enter image description here

Upvotes: 4

sriram neelamegam
sriram neelamegam

Reputation: 1

Set the content insets to zero:

Upvotes: -1

Jacobo Koenig
Jacobo Koenig

Reputation: 12514

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.

Upvotes: 7

NitinLandge
NitinLandge

Reputation: 11

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.sstatic.net/vRNfV.png

Upvotes: 0

Dew Time
Dew Time

Reputation: 878

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.

Upvotes: 2

jr.ong
jr.ong

Reputation: 11

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

Upvotes: 1

netshark1000
netshark1000

Reputation: 7403

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.

Upvotes: 50

lekksi
lekksi

Reputation: 4868

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"

Upvotes: 379

Austin Whitelaw
Austin Whitelaw

Reputation: 107

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!

Upvotes: 1

Vinod Joshi
Vinod Joshi

Reputation: 7852

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

Upvotes: 0

Patricio Diego
Patricio Diego

Reputation: 11

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"

Upvotes: 0

Anusha Kottiyal
Anusha Kottiyal

Reputation: 3905

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
}

Upvotes: 3

Carl
Carl

Reputation: 2934

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.

Upvotes: 0

Sandeep Ahuja
Sandeep Ahuja

Reputation: 931

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

Upvotes: 0

Arthi
Arthi

Reputation: 128

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

Upvotes: 2

Daniel Meza
Daniel Meza

Reputation: 1

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.

Upvotes: 0

Manikandan
Manikandan

Reputation: 253

check for

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

Upvotes: -2

Sebyddd
Sebyddd

Reputation: 4315

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.

Upvotes: 0

Andre Cytryn
Andre Cytryn

Reputation: 2705

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

Upvotes: 9

Alexander
Alexander

Reputation: 9081

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.

Upvotes: 0

akaDuality
akaDuality

Reputation: 408

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

Upvotes: 5

Junaid Ahmed
Junaid Ahmed

Reputation: 175

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

Upvotes: 1

irblue
irblue

Reputation: 851

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.

Upvotes: 73

Related Questions