I am using a UITableviewcontroller
and I want the footer view to always stay at the bottom of the screen. However, the y
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.
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)
Add a ViewController
in Storyboard and set your footer view(or button) at the bottom of the ViewController
.
Add a ContainerView
in the ViewController
and set constraint
Add a UITableViewController
and embedded the tableview in the container view
Approach 2: (Available for both Dynamic TableView)
Add a ViewController
, set footer view in the bottom, add UITableView and set your auto layout.
You can't set static tableview in a UIViewController
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
:
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:
The end result is this:
Full project code is here:
https://www.dropbox.com/s/pyp42nzumquompp/TableViewFooter.zip?dl=0
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:
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
}