Guilherme Torres Castro
Guilherme Torres Castro

Reputation: 15350

UITableView extra space at bottom

My tableview has an extra space at the bottom, as you see in the picture:

All the rows in the tableView have a fixed height of 71pt.

enter image description here

Upvotes: 24

Views: 47239

Answers (11)

Mehrdad
Mehrdad

Reputation: 1208

My problem didn't solve by the answers. i had a UISearchbar as tableHeaderView which caused the white space ( i don't know why) . I removed the searchbar from tableview in storyboard and in viewWillLayoutSubviews added in table header then problem solved.

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    tableView.tableHeaderView = searchBar
}

Upvotes: 1

Brian Hong
Brian Hong

Reputation: 1084

I had the same issue, with latest Xcode (11.3) with latest iOS (13.3) iPhone. I tried many answers, but none worked.

My tableView has top, bottom, leading, and trail constraints but somehow there is large space at bottom below the last cell. (tableView.contentSize.height was almost double of what is needed) Data for tableView is not dynamic at all.It happens both when the view controller that has it shows it and when tableView is hidden then shown again.

I solved it by using scrollView's delegate method like below, so that when the user starts dragging, height is adjusted. For me, it works so great, hope for you, too!

// this is the best place to adjust the contentSize.height of tableView.
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    print("scrollViewWillBeginDragging():") // optional
    // replace allParkingGeos.count with your data for tableView
    tableView.contentSize.height = CGFloat(allParkingGeos.count) * cellHeight
}

Upvotes: 3

Dmitry
Dmitry

Reputation: 607

The accepted solution is no longer a correct one if it comes to iOS 11 and Xcode 9. To achieve similar effect you have to go to Size Inspector where you can find this:

enter image description here

You can achieve the same in code:

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

Upvotes: 2

Allison
Allison

Reputation: 2383

@urgentx's answer did most of what I wanted but didn't get all the way there. I'm in a similar situation (upside down UITableView for a chat app) however their suggestion fully disables safe area behavior which isn't desirable on devices like the iPhone X because it means the tableview will be clipped by both the home indicator and any top navigation bars.

I did the following in order to make the tableview still correctly avoid safe areas while inverted:

override func viewDidLoad() {
    super.viewDidLoad()
    self.automaticallyAdjustsScrollViewInsets = false
    self.tableView.contentInsetAdjustmentBehavior = .never
}

and then:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    //fetch the safe area in layoutSubviews since this it's possible that they can change when the device is rotated
    let safeArea = self.tableView.safeAreaInsets
    //insets are **flipped** because the tableview is flipped over its Y-axis!
    self.tableView.contentInset = .init(top: safeArea.bottom, left: 0, bottom: safeArea.top, right: 0)
}

This in essence replicates contentInsetAdjustmentBehavior = .automatic behavior but with the insets flipped across the Y-axis.

Upvotes: 5

Varsha Shivhare
Varsha Shivhare

Reputation: 91

in this case you can give tableview the bottom space 0 to its superview and it will work i think or you can do,

tableView.tableFooterView = UIView();

or you can do like below,

The lower value should be 1.0.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
    if(section == 0) {
       return 6;
    } else {
       return 1.0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
    return 5.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

Upvotes: 2

Yonatan Vainer
Yonatan Vainer

Reputation: 453

In case there is an embedded navigation controller, checking "shows toolbar" creates bottom spacing in the view controller. Just uncheck it.

enter image description here

Upvotes: 1

iOS Unit
iOS Unit

Reputation: 218

In my case tableView was created programmaticaly and I had to set tableView.estimatedRowHeight to make the space disappear

Upvotes: 6

Saeed Ir
Saeed Ir

Reputation: 2332

This is how it can be fixed easily through Storyboard (iOS 11):

Select Table View > Size Inspector > Content Insets: Never

Upvotes: 4

urgentx
urgentx

Reputation: 3931

I had this issue when I flipped my UITableView upside-down for a chat messaging screen.

Before I flipped the table view, the content inset was leaving a blank space at the top so that when we scrolled all the way up, the content was not obstructed by the navigation bar. Once I flipped it the inset was moved to the bottom.

Change your 'Content Insets' to Never in the Size Inspector of your UITableView to get rid of this space at the end.

Upvotes: 2

Integrating Stuff
Integrating Stuff

Reputation: 5303

In my case this was due to having a footer of height 18 by default at the bottom of the UITableView. Setting it to 1 removed the extra space at the bottom.

Upvotes: 4

Guilherme Torres Castro
Guilherme Torres Castro

Reputation: 15350

Ok, I got it.

The "Adjust Scroll View Insets" was marked on my viewController (not my tableView).

Here is an answer with a detailed explanation

Upvotes: 37

Related Questions