如何在iOS中的表视图底部创建固定单元格?
我想在iOS的表格视图底部制作一个固定的单元格,类似于HQ trivia iOS app的排行榜视图。
喜欢这个:
为了给出固定单元格的印象,您可以简单地将UITableView
添加到常规UIViewController
,设置其约束以使其消耗整个视图,但是从屏幕底部停止(例如)60px。
使用与单元格具有相同UI的UIView
填充剩余空间……就这样,桌面视图将滚动,并且“单元格”将始终在底部可见。
我相信最简单,最干净的方法是在viewController
的主view
中添加一个类似“cell”的子view
,并使用autolayout约束使其在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), ])
正如西蒙提到的那样,我将页脚视图放在滚动视图的底部。 我为页脚视图留出空间,以便在约束下固定在底部。
例