How can I make the footerview always stay at the bottom in UITableViewController?

廉价感情. 提交于 2019-12-05 17:30:27

问题


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?


回答1:


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.




回答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:

The end result is this:

Full project code is here:

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




回答3:


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




回答4:


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
}


来源:https://stackoverflow.com/questions/34610044/how-can-i-make-the-footerview-always-stay-at-the-bottom-in-uitableviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!