I have got a View Controller embedded in a Navigation Controller with prefers large titles option set to true; inside the View Controller there’s a Scroll View.
I wa
In case someone needs to have some view on top, like a menu, and then a UITableView/UICollectionView, and the large title is not hiding when scrolling, like in this "awesome" picture I made:
You will need to set up all the required constraints for the UIView & for your UITableView/UICollectionView, but the most vital part is:
make sure your UITableView/UICollectionView is the first subview, and then the UIView or any other views are behind.
Here is an example of that:
I tested with storyboard approach, it is working. Make sure to set the scrollView & it's subviews Constraints.
Here with the test sample XCode project for references: https://github.com/DazChong/LargeTitleShrinkOnScrollView
This way is worked for me. i followed these steps: 1- Moving the TableView to top of the list (under safe area) in view controller scene. 2- Making FirstView and Loaded by FirstViewContoller.
The screenshot of the solution
Try something like this:
extendedLayoutIncludesOpaqueBars = true
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor)
])
I've used something that is similar to Kamil Szostakowski's answer for view with UICollectionView.
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
UIView.animate(withDuration: 0.5, animations: {
self.navigationController?.navigationBar.prefersLargeTitles = (velocity.y < 0)
})
}
It may be not so smooth but I haven't found better option yet.
I have not achieved this using a UIScrollView but I achieved it with other ViewControllers using a UITableView as first view.
If the tableView is not the first view, the large title fails to hide automatically. You most likely need to make sure your tableView is the first element in the main view’s subviews array.
I hope that this solves your problem.