I have a tableView.tableHeaderView which is a UITableViewHeaderFooterView.
I would like this header to initially not be displayed when the tableViewController is first presented. When the user scrolls down from the top of the tableView, I would like to present the header.
Then when the user slightly scrolls down, I want the header to snap to a hidden position. The behaviors is exactly like the archived chats header of the WhatsApp page where all your chat's are listed.
Is there any way to achieve this without a complex set of scrollview delegate calls?
I thought on previous versions of swift/xcode, the tableView.tableHeaderView kind of snapped up and down but I notice it's not doing that anymore.
I think the only solution might be overriding the scrollViewDidScroll. This is what I've done but when the tableView.headerView reappears, it does so over the first cell of the tableView. Not sure how to make it appear in the correct position.
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let myHeaderView = self.tableView.tableHeaderView as? MyTableViewHeaderFooterView {
let height = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height + self.searchController.searchBar.frame.size.height
if scrollView.contentOffset.y < -height {
UIView.animate(withDuration: 0.1) {
myHeaderView.frame.size.height = 44
}
}
}
}