I could not find any proper solution for this issue so I've added my searchbar to my main view and synchronized its height value with my collection views didscroll event.
Here is the code snippet if anyone is interested.
let offset = self.collectionView.contentOffset
let y = offset.y
if lastContentOffset > scrollView.contentOffset.y && lastContentOffset < scrollView.contentSize.height - scrollView.frame.height {
// move up
if self.cst_searchBarHeight.constant == 0 && y <= 0{
self.cst_searchBarHeight.constant = self.searchBarHeight
UIView.animate(withDuration: 0.4, delay: 0.0, options: [.curveEaseInOut], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
} else if lastContentOffset < scrollView.contentOffset.y && scrollView.contentOffset.y > 0 {
// move down
if self.cst_searchBarHeight.constant != 0{
self.cst_searchBarHeight.constant = 0
UIView.animate(withDuration: 0.4, delay: 0.0, options: [.curveEaseInOut], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
// update the new position acquired
lastContentOffset = scrollView.contentOffset.y
}
I've got the idea from the below answer which finds out the scroll direction of collection view
How can I detect the scroll direction from the UICollectionView?