UITableView底部出现额外的空间

23
我的tableView底部多了一个空白区域,如图所示:
tableView中所有行的高度都是固定的71pt。 enter image description here

当您从那里向上拉视图时,滚动条似乎会在哪里弹跳? - Tommy
@Tommy 在图片的同一位置 - Guilherme Torres Castro
只有这个答案对我有帮助。 - Jerome
11个回答

37

好的,我明白了。

“调整滚动视图插入”被标记在我的viewController上(而不是我的tableView)。

这里有一个答案,其中包含了详细的解释。


6
在我的情况下,tableView 是通过编程创建的,我必须设置 tableView.estimatedRowHeight 以消除空间。

5

@urgentx的答案基本满足我的需求,但并没有完全解决问题。我也遇到了类似的情况(聊天应用中的倒置UITableView),但是他们的建议完全禁用了安全区域行为,这对于像iPhone X这样的设备并不理想,因为这意味着tableview将被下方的home指示器和任何顶部导航栏裁剪。

为了使tableview在倒置时仍能正确避开安全区域,我进行了以下操作:

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

然后:
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)
}

这实质上复制了contentInsetAdjustmentBehavior = .automatic的行为,但将插图沿Y轴翻转。

4
在我的情况下,这是由于在UITableView底部默认具有高度为18的页脚所致。将其设置为1可删除底部多余的空间。

4
这是如何通过Storyboard(iOS 11)轻松解决的方法:
选择Table View > Size Inspector > Content Insets: Never。

3

我曾经遇到过相同的问题,在最新版本的Xcode (11.3) 和 iOS (13.3) iPhone上。我尝试过很多解决方法,但都没有奏效。

我的 tableView 有上、下、左、右约束,但是在最后一个单元格下方有很大的空间。(tableView.contentSize.height 几乎是所需高度的两倍)。tableView 的数据并不是动态的。这种情况发生在显示它的视图控制器显示和 tableView 再次隐藏时再次显示时都会发生。

我通过使用scrollView的代理方法来解决这个问题,当用户开始拖动时,高度自动调整。对我而言,这个方法非常有效,希望对你也有用!

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

感谢Brian Hong,它完美地解决了我的问题。但我仍然对这个问题很好奇。 在我的情况下,我有一个从SQLite加载数据的tableview。当没有searchBar时,它正常工作,但当我将searchBar添加为UITableview header时,就会出现这个问题。 - KinhChenDev

2
在这种情况下,您可以将tableview的底部空间设置为其父视图的0,我认为它会起作用,或者您可以执行以下操作: tableView.tableFooterView = UIView(); 或者您可以像下面这样做,
较低的值应该为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)];
}

2

如果涉及到iOS 11和Xcode 9,那么已经被接受的解决方案不再正确。为了达到类似的效果,您需要进入“尺寸检查器”中找到以下内容:

enter image description here

您也可以在代码中实现相同的效果:

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

1
如果存在嵌入式导航控制器,勾选“显示工具栏”会在视图控制器中创建底部间距。只需取消勾选即可。

enter image description here


1

当我将UITableView翻转用于聊天消息屏幕时,遇到了这个问题。

在翻转TableView之前,内容插入会留下一个空白空间在顶部,这样当我们滚动到最上方时,内容不会被导航栏遮挡。一旦翻转,插入就移动到底部。

在您的UITableView的Size Inspector中将“Content Insets”更改为Never,以消除末尾的空白空间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接