0

I have a filterbar, which is a UIView as well as a tableView in my controller like so:

override func viewWillLayoutSubviews() {
    sortSalonsByDistanceAndReload()
}    

func setupViews() {
    view.addSubview(filterBar)
    view.addSubview(tableView)

    filterBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    filterBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    filterBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

    filterBarHeightLC = filterBar.heightAnchor.constraint(equalToConstant: 44)
    filterBarHeightLC?.isActive = true

    tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: filterBar.bottomAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

}

I want to make the filterBar dissapear when the user scrolls down on the tableView, and then reappear when the user scrolls up

luke
  • 2,743
  • 4
  • 19
  • 43

2 Answers2

0

You can use Search Controller. Take a look at How to implement UISearchController in UITableView - SWIFT

if it doesn't work out for you , you could add as a section or part of header view

Community
  • 1
  • 1
Vignesh
  • 10,205
  • 2
  • 35
  • 73
0

The delegate of your tableView should implement the UIScrollViewDelegate method scrollViewDidScroll: and query the contentOffset.y property on the scrollView (your tableView). Then it depends on when you want the bar to appear: only on the top, or also whenever the user scroll up inside the tableView. You could sync the contentOffset.y difference with the filterBars top constraint's constant (ranging from -44 to 0). It might be better to inset your tableView and update that inset as well (from 0 to 44). If your filterBar could be implemented with a navigation bar from a UINavigationController you might just do navigationController?.hidesBarsOnSwipe = true, but I don't know your view controller setup.

fruitcoder
  • 1,073
  • 8
  • 24