0

I have a UITableView with a bunch of custom cells (and each cell has a custom height and dynamic height).

I want to add a footer view to the tableview that will always be on the bottom of the tableView contentSize.

If the contentSize is bigger than the screen size so in the bottom of the tableView (you scroll down, like a regular last cell).

If the contentSize is smaller than the screen size so the footer will still be in the bottom of the screen and there will be a gap from the last cell to the footer.

I've tried to add a footer like this:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let myFooterView = Bundle.main.loadNibNamed("MyFooterView", owner: self, options: nil)?.first as! MyFooterView
    myFooterView.delegate = self
    myFooterView.frame = CGRect(x: 0, y: 0, width: self.tableView.frame.size.width, height: 100)
    return myFooterView
}

But the footer is floating in on the bottom when I scroll. Tha'ts not what I want to achieve.

Any idea how can I achieve a non-floating footer like I want?

Thanks! :)

F.SO7
  • 695
  • 1
  • 13
  • 37
  • Add your footer as additional `UITableViewCell` – Reinier Melian Sep 12 '17 at 10:53
  • @ReinierMelian I've thought about it. But like I said: "If the contentSize is smaller than the screen size so the footer will still be in the bottom of the screen and there will be a gap from the last cell to the footer.". It won't work with an additional cell – F.SO7 Sep 12 '17 at 10:54
  • Does this answer your question? [UITableView Footer, Stop from floating over content](https://stackoverflow.com/questions/5740518/uitableview-footer-stop-from-floating-over-content) – Ajith Renjala Jan 25 '21 at 21:05

1 Answers1

2

Add a view as a subview to the table view and adjust the position in scrollView:didScroll:. To be able to do this, add conformance to UIScrollViewDelegate to your view controller. I think a UITabelViewController is automatically the delegate for the table view (which is also a UIScrollView).

The tricky part is the calculation of the position based on the content size, the content offset, the size of the view and the size of your footer.

dasdom
  • 13,975
  • 2
  • 47
  • 58
  • Hi. Thanks for your answer. Can you explain a bit more? How can I adjust it in the delegate function and to what value? – F.SO7 Sep 12 '17 at 10:56
  • I know how to add the delegate method. But to what value should I set the y position of the footer subview? – F.SO7 Sep 12 '17 at 11:00
  • You have to calculate the position based on the size of the table view, the size of the footer, the content size and the content offset. If you have difficulties to wrap your head around this, get yourself a sheet of paper and draw the screen with all these different values. You can do this! – dasdom Sep 12 '17 at 11:02
  • But if the view will be less than the screen how can I add the gap? And how to contentSize will be updated? And if the screen is bigger the footer will be above other cells... – F.SO7 Sep 12 '17 at 11:03
  • 1
    If the content size in less than the view size, adjust you calculation such that the view is at the bottom of the screen. And look up what `contentInset` does. Seriously, you should draw that on a sheet of paper. And experiment, experiment, experiment. – dasdom Sep 12 '17 at 11:06