【问题标题】:How can I make the fixed cell at the bottom of the table view in iOS?如何在 iOS 的表格视图底部制作固定单元格?
【发布时间】:2018-08-08 13:03:01
【问题描述】:

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

像这样:

【问题讨论】:

  • 这可能只是一个位于表格视图下方的视图,并且看起来像一个单元格。
  • 您可以将页脚视图添加到表格中,因此它将固定并浮动在表格视图上
  • @MikeTaverne 我明白了。谢谢。
  • @Sumanth 会试试的。

标签: ios uitableview user-interface uiscrollview user-experience


【解决方案1】:

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

UIView 填充剩余空间,该UIView 与单元格具有相同的用户界面......就是这样,表格视图将滚动,并且“单元格”将始终在底部可见。

【讨论】:

  • 是否有任何理由将其设置为 60px?这是标准值吗?
  • @SawThinkarNayHtoo 不,正如我在回答中所说,60 只是一个示例,您需要将其设置为适合您需要的任何内容/ui
  • 明白了!谢谢!
【解决方案2】:

我相信实现这一目标的最简单和最干净的方法是在viewController 的主view 中添加一个类似“单元格”的子视图,并使用自动布局约束将其固定在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),
    ])

【讨论】:

  • 感谢您的回答。我也会尝试这种方式,目前,我只是使用故事板 UI 组件进行测试。
【解决方案3】:

正如西蒙所说,我将页脚视图放在滚动视图的底部。我为页脚视图留出空间,以便将其固定在底部并带有约束。

Example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 2014-05-22
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多