简体   繁体   中英

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.

在此处输入图片说明

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

在我的情况下,tableView 是以编程方式创建的,我必须设置tableView.estimatedRowHeight以使空间消失

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.

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

Select Table View > Size Inspector > Content Insets: Never

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

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
}

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.

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

在此处输入图片说明

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

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:

在此处输入图片说明

You can achieve the same in code:

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM