0

I'm trying to display a "that's all" message to the user if the user reaches the bottom of the table view but keeps scrolling up, like the "you're up to date" message slack displays at the bottom of the chat. However, the tableFooterView can be seen at the very bottom of the table view and isn't hidden. How should this be done?

Pirlo Lochisomo
  • 185
  • 1
  • 16

3 Answers3

1

I use this solution:

let test=UILabel(frame: CGRect(x: 0,y: tableView.contentSize.height+180, width: tableView.frame.width, height: 50))
test.text="That's all"
view.insertSubview(test, belowSubview: tableView)
omarzl
  • 589
  • 4
  • 9
0

Adding a footerview won't work because then the tableview will adjust its contentSize to display it.

You can add a subview directly to the UITableView, set its frame's origin.y to be greater than the contextSize.y. Whenever you add or remove rows, or add or remove section section or reload the table you have to readjust the view.

Jon Rose
  • 8,373
  • 1
  • 30
  • 36
0

I faced the same problem and the above solution did not work for me.

I ended up using auto layout constraints. Initially the footer view's bottom anchor is set a bigger constrant to keep it invisible

Then used begin & end dragging methods of scroll view delegate to show & hide it

extension ViewController: UITableViewDelegate {
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        guard let bottomAnchor = self.bottomAnchor else { return }
        guard scrollView.contentSize.height > scrollView.frame.size.height else { return }
        let heightOfInvisibleContent = (scrollView.contentSize.height - scrollView.frame.size.height)
        print("height of invisible content: \(heightOfInvisibleContent), offset: \(scrollView.contentOffset.y)")
        guard scrollView.contentOffset.y >= heightOfInvisibleContent else { return }
        bottomAnchor.constant = moveUpConstant
        UIView.animate(withDuration: 0.5) {
            self.view.layoutIfNeeded()
        }
    }

    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        guard let bottomAnchor = self.bottomAnchor else { return }
        bottomAnchor.constant = moveDownConstant
        UIView.animate(withDuration: 0.5) {
            self.view.layoutIfNeeded()
        }
    }
}

I have shared my full sample project (FooterMessage) in my repo https://github.com/ramjyroo/iOS-Expedition

ram
  • 78
  • 1
  • 8