@JordanWood's answer caused a visual bug when popping VC from navigation stack back to the screen with SearchBar, so i came up with solution that fixed it
NOTE: This solution seems to be reasonable if your collection content will never overgrow self.view
's frame, otherwise u'll need to tune it a bit to get correct dynamic collectionViewContentOffset
. i.e when user leaves the screen which is scrolled far down and comes back, collectionView
will appear scrolled to top, which is mostly not what you want
private var collectionViewContentOffset: CGPoint?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationItem.hidesSearchBarWhenScrolling = false
collectionView.setContentOffset(
collectionViewContentOffset ?? .zero,
animated: false
)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationItem.hidesSearchBarWhenScrolling = true
if collectionViewContentOffset == nil {
collectionViewContentOffset = CGPoint(
x: 0,
y: -view.safeAreaInsets.top
)
}
}
Another good solution i found (which works fine when collection's contentSize is bigger than self.view
's frame) is to run it like that:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesSearchBarWhenScrolling = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationItem.hidesSearchBarWhenScrolling = true
}
Downside of that option is that your searchBar will hide when you try to do a swipe up gesture on a collectionView
(collectionView's
contentSize
needs to be smaller than self.view
's frame), leave the screen and come back. But there are no visual bugs, just the fact that it hides in one corner case
hidesSearchBarWhenScrolling
– ProbablyfirstResponder
? – LanceyscrollView.setContentOffset(_:animated)
. Anyone have a suggestion? – FoliolatehidesSearchBarWhenScrolling = false
puts the search bar over large title in iOS 13. Any idea if I can update this somehow? – Selby