简体   繁体   中英

swift how to display a footer that can't be seen unless scrolled up further after reaching bottom?

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?

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)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM