I am using a UITableviewcontroller
and I want the footer view to always stay at the bottom of the screen. However, the y
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