[ios] 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?

This question is related to ios objective-c uitableview ios7

The answer is


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!


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
}

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


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

self.extendedLayoutIncludesOpaqueBars = true

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.


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.


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)

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.


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


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.


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

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


Set the content insets to zero:


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.


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.


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.


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

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.


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

enter image description here


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.


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


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.


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


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


check for

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

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.


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


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


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"

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to objective-c

Adding a UISegmentedControl to UITableView Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Warp \ bend effect on a UIView? Use NSInteger as array index Detect if the device is iPhone X Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3 ITSAppUsesNonExemptEncryption export compliance while internal testing? How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem? Change status bar text color to light in iOS 9 with Objective-C

Examples related to uitableview

Adding a UISegmentedControl to UITableView make UITableViewCell selectable only while editing How to fix Error: this class is not key value coding-compliant for the key tableView.' UITableView example for Swift Swift - how to make custom header for UITableView? How to insert new cell into UITableView in Swift How to detect tableView cell touched or clicked in swift Dynamic Height Issue for UITableView Cells (Swift) UIButton action in table view cell How to get the indexpath.row when an element is activated?

Examples related to ios7

Xcode: Could not locate device support files How to Apply Gradient to background view of iOS Swift App How do I hide the status bar in a Swift iOS app? Using Predicate in Swift How do I programmatically set device orientation in iOS 7? What is the height of Navigation Bar in iOS 7? Warning :-Presenting view controllers on detached view controllers is discouraged Color Tint UIButton Image iOS change navigation bar title font and color How to embed small icon in UILabel