在需要上滑隐藏的tableView或者scrollView中,实现scrollViewWillEndDragging
方法:
首先在控制器中设一个保存上一个偏移量的属性:
TestTableViewController:
fileprivate var tempY: CGFloat = -64
override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if velocity.y > 0 || scrollView.contentOffset.y > self.tempY {
self.navigationController?.setNavigationBarHidden(true, animated: true)
} else {
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
self.tempY = scrollView.contentOffset.y
}