ios 如何使页脚视图始终位于 UITableViewController 的底部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34610044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make the footerview always stay at the bottom in UITableViewController?
提问by Isha Balla
I am using a UITableviewcontroller
and I want the footer viewto always stay at the bottomof the screen. However, the y
position of the footer viewis changing as per the height
of the tableviewcell
. How can I always make it stick to the bottom of the screen?
我正在使用 aUITableviewcontroller
并且我希望页脚视图始终位于屏幕底部。然而,y
所述的位置页脚视图正在改变,因为每height
的tableviewcell
。我怎样才能让它一直粘在屏幕底部?
回答by MeghaJain
The tableview
footer view would always stay at the bottom of the tableview
and would always scroll with it. If you need to make the footer view fixed at the bottom then you can not use a TableViewController
.You will have to use UIViewController
, put your tableView
as a subview. Put the footer also as another subview and its done.
在tableview
页脚视图将永远留在的底部tableview
,并总是用它滚动。如果您需要将页脚视图固定在底部,则不能使用TableViewController
。您必须使用UIViewController
, 将您的tableView
作为子视图。将页脚也作为另一个子视图并完成。
回答by Ankur Lahiry
A footer is always added to the bottom of the content size. So my approach is
页脚总是添加到内容大小的底部。所以我的方法是
Approach 1: (Available for Both Static and Dynamic TableView)
方法一:(静态和动态TableView都可用)
Add a ViewController
in Storyboard and set your footer view(or button) at the bottom of the ViewController
.
ViewController
在 Storyboard 中添加一个并将页脚视图(或按钮)设置在ViewController
.
Add a ContainerView
in the ViewController
and set constraint
添加ContainerView
在ViewController
与约束集
Add a UITableViewController
and embedded the tableview in the container view
UITableViewController
在容器视图中添加并嵌入tableview
Approach 2: (Available for both Dynamic TableView)
方法二:(动态TableView都可用)
Add a ViewController
, set footer view in the bottom, add UITableView and set your auto layout.
添加一个ViewController
,在底部设置页脚视图,添加 UITableView 并设置您的自动布局。
You can't set static tableview in a UIViewController
你不能在 UIViewController 中设置静态 tableview
回答by David S.
You cannot use a UITableViewController
, but it's not necessary anyway. You can easily add a UITableView
to a regular UIViewController
, and just use auto layout to add a UIView
below the UITableView
. Here's a simple example, adding a red UIView
below the UITableView
:
您不能使用 a UITableViewController
,但无论如何都没有必要。您可以轻松地将 a 添加UITableView
到常规UIViewController
,只需使用自动布局UIView
在UITableView
. 这是一个简单的例子,在UIView
下面添加一个红色UITableView
:
import UIKit
class MyFooterTableViewController: UIViewController {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var data = [ "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
}
@IBOutlet weak var tableView: UITableView!
}
extension MyFooterTableViewController: UITableViewDataSource {
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCellTest", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
cell.detailTextLabel?.text = "BANANA"
return cell
}
}
The constraints on the Storyboard look like this:
Storyboard 上的约束如下所示:
The end result is this:
最终结果是这样的:
Full project code is here:
完整的项目代码在这里:
https://www.dropbox.com/s/pyp42nzumquompp/TableViewFooter.zip?dl=0
https://www.dropbox.com/s/pyp42nzumquompp/TableViewFooter.zip?dl=0
回答by Chen Jiling
Swift Code which make the footerView fill the rest space at the bottom, then you can use autolayout to make you view at the bottom in the footerView:
使footerView填充底部剩余空间的Swift代码,然后您可以使用自动布局使您在footerView底部查看:
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
guard let footerView = tableView.tableFooterView else
{
return
}
var cellsHeight:CGFloat = tableView.tableHeaderView?.frame.height ?? 0
let sections = self.numberOfSections(in: tableView)
for section in 0..<sections
{
let rows = self.tableView(tableView, numberOfRowsInSection: section)
for row in 0..<rows
{
let indexPath = IndexPath(item: row, section: section)
cellsHeight += self.tableView(tableView, heightForRowAt: indexPath)
}
}
var frame = footerView.frame
frame.size.height = tableView.contentSize.height - cellsHeight;
frame.origin.y = cellsHeight
footerView.frame = frame
}