andreamazz /AMScrollingNavbar
Scrollable UINavigationBar that follows the scrolling of a UIScrollView
★ 5,980⑂ 626👁 126⑃ 104● 306▤ 51
+0
日增长
5,980
stars
626
forks
126
watchers
104
prs
306
issues
51
releases
README
!Build status
!Swift 5
A custom UINavigationController that enables the scrolling of the navigation bar alongside the
scrolling of an observed content view
Versioning notes
- Version 2.x is written as a subclass of UINavigationController, in Swift.
- Version 2.0.0 introduced Swift 2.0 syntax.
- Version 3.0.0 introduced Swift 3.0 syntax.
- Version 4.0.0 introduced Swift 4.0 syntax.
- Version 5.1.0 introduced Swift 4.2 syntax.
If you are looking for the category implementation in Objective-C, make sure to checkout version 1.x and prior, although the 2.x is recomended.
Screenshot
Setup with CocoaPods
pod 'AMScrollingNavbar'
use_frameworks!
Setup with Carthage
github "andreamazz/AMScrollingNavbar"
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
swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50.0)
}
}
Objective-C
objc
- (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:
swift
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.stopFollowingScrollView()
}
}
ScrollingNavigationViewController
To DRY things up you can let your view controller subclass ScrollingNavigationViewController, which provides the base setup implementation. You will just need to call followScrollView(_: delay:):
swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50.0)
}
}
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:
swift
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50Top issues
#335Fully Hide Follower(s) and content inset issues35 comments · 2018-10-14#302Bottom View popups issue on showNavBar when followerView exists21 comments · 2018-03-06#389scroll to top doesn't work correctly11 comments · 2020-01-19#406No more scrolling with iOS 15.1 10 comments · 2021-10-27#311ScrollView sometimes doesn't get back to it's default position.10 comments · 2018-04-09#285ios11 issue10 comments · 2017-10-26#366Web view's content behind navigation bar when returning from safari view controller8 comments · 2019-07-24#353Add option for pre-v5.1.0 custom followers behaviour8 comments · 2018-11-01
Latest release
Release 5.3.0 5.3.0 2019-07-16- Merged 365