andreamazz/AMScrollingNavbar
Scrollable UINavigationBar that follows the scrolling of a UIScrollView
Usage
Make sure to use ScrollingNavigationController instead of the standard UINavigationController. Either set the class of your UINavigationController in your storyboard, or create programmatically a ScrollingNavigationController instance in your code.
Use followScrollView(_: delay:) to start following the scrolling of a scrollable view (e.g.: a UIScrollView or UITableView).
Swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50.0)
}
}Objective-C
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[(ScrollingNavigationController *)self.navigationController followScrollView:self.tableView delay:0 scrollSpeedFactor:1 collapseDirection:NavigationBarCollapseDirectionScrollDown followers:nil];
}Use stopFollowingScrollview() to stop the behaviour. Remember to call this function on disappear:
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.stopFollowingScrollView()
}
}Followers
To move another view, like a toolbar, alongside the navigation bar you can provide the view or multiple views as the followers parameter. Since you might want to have the follower up or down, you'll have to specify the scroll direction of the view once it starts to follow the navigation bar:
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50.0, followers: [NavigationBarFollower(view: customFooter, direction: .scrollDown)])
}Note that when navigating away from the controller the followers might keep the scroll offset. Refer to Handling navigation for proper setup.
Additional scroll
If you want to furhter scroll the navigation bar out of the way, you can use the optional parameter additionalOffset in the followScrollView call.
Scrolling the TabBar
You can also pass a UITabBar in the followers array:
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50.0, followers: [tabBarController.tabBar])
}Scrolling to top
When the user taps the status bar, by default a scrollable view scrolls to the top of its content. If you want to also show the navigation bar, make sure to include this in your controller:
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.showNavbar(animated: true, scrollToTop: true)
}
return true
}Scroll speed
You can control the speed of the scrolling using the scrollSpeedFactor optional parameter:
controller.followScrollView(view, delay: 0, scrollSpeedFactor: 2)Check out the sample project for more details.
Package Metadata
Repository: andreamazz/AMScrollingNavbar
Stars: 6004
Forks: 632
Open issues: 38
Default branch: master
Primary language: swift
License: MIT
README: README.md