14

I am using a UITableviewcontroller and I want the footer view to always stay at the bottom of the screen. However, the y position of the footer view is changing as per the height of the tableviewcell. How can I always make it stick to the bottom of the screen?

Lukesivi
  • 2,206
  • 4
  • 25
  • 43
Isha Balla
  • 733
  • 2
  • 9
  • 25
  • Use tableFooterView property of table view – Mehul Sojitra Jan 05 '16 at 11:04
  • override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { if section == 1 { let footerView = UIView(frame: CGRectMake(0, UIScreen.mainScreen().bounds.height - 40, tableView.frame.size.width, 40)) return footerView } return UIView() } This is actually my block of code. I want the footer at the bottom of the screen but it is not sticking at the bottom of the screen and keeps varying according to the height of tableview – Isha Balla Jan 05 '16 at 11:09
  • Edited : = I dont think it was possible with uitableviewcontroller.. So what I did was added the view as a subview of navigation controller and it worked for me :) – Isha Balla Jan 05 '16 at 11:45

6 Answers6

7

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.

Rohit Pradhan
  • 3,867
  • 1
  • 21
  • 29
MeghaJain
  • 164
  • 1
  • 1
  • 8
  • I know it sounds stupid. But is it possible to add another a view in tableviewcontroller? Here m working on something for which I have to use tableviewcontroller so can't switch to a view controller. I am trying to make it work :( is it possible to add any other view in tableviewcontroller class? And maybe make it stick to the bottom – Isha Balla Jan 05 '16 at 11:28
  • If you are using a `UINavigationController`, you an add your `footerview` as a `subview` to your `navigationController`. Don't forget to remove the `footerView` from `viewDidDisappear`, otherwise it'll appear with all of your screens in the navigation stack. – user1107173 Nov 22 '17 at 19:23
2

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

Ankur Lahiry
  • 2,253
  • 1
  • 15
  • 25
0

If you want to keep footerview always at the bottom of the tableview without floating property like footer for section then can use

tableView.tableFooterView = customFooterView

Khushboo Dhote
  • 198
  • 3
  • 13
-1

Override existing method of UiTableView

func allowsFooterViewToFloat() -> Bool { return true }

Urvish Modi
  • 1,118
  • 10
  • 11
-2

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
}
Chen Jiling
  • 519
  • 5
  • 10
-2

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:

constraints

The end result is this:

animated

Full project code is here:

https://www.dropbox.com/s/pyp42nzumquompp/TableViewFooter.zip?dl=0

David S.
  • 6,567
  • 1
  • 25
  • 45