如何在 iOS 中的 table 视图底部制作固定单元格?

How can I make the fixed cell at the bottom of the table view in iOS?

我想在 iOS 中的 table 视图底部创建一个固定单元格,类似于 HQ trivia iOS 应用程序的排行榜视图。

像这样:

为了给人一种固定单元格的印象,您只需将 UITableView 添加到常规 UIViewController 上,设置其约束,使其占用整个视图,但停止(例如)距离屏幕底部 60px。

用与单元格具有相同 UI 的 UIView 填充剩余的 space ...就是这样,tableview 将滚动,并且 "cell" 将始终显示在底部。

我相信实现这一目标的最简单和最干净的方法是向 viewController 的主 view 添加一个类似于 "cell" 的子视图,并使用自动布局约束来实现它固定在tableView:

NSLayoutConstraint.activate([
    tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
    tableView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
    tableView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor),
    // this will make sure tableView will be placed above bottomFixedFooter, and bottomFixedFooter won't overlay any content of the tableView
    bottomFixedFooter.topAnchor.constraint(equalTo: tableView.bottomAnchor),

    bottomFixedFooter.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
    bottomFixedFooter.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor),
    bottomFixedFooter.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),

    // explicitly set the height of the bottomFixedFooter, if it does not have intrinsic content size (or its size is not calculated
    // internally using autolayout)
    bottomFixedFooter.heightAnchor.constraint(equalToConstant: 50),
    ])

正如 Simon 提到的,我将页脚视图放在滚动视图的底部。我将 space 留给页脚视图,以便它固定在具有约束的底部。

Example