一个自定义的 UINavigationController,它能够随着被观察的内容视图的滚动而滚动导航栏。
2.x
是用 Swift 编写的 UINavigationController
的子类。2.0.0
引入了 Swift 2.0 语法。3.0.0
引入了 Swift 3.0 语法。4.0.0
引入了 Swift 4.0 语法。5.1.0
引入了 Swift 4.2 语法。如果您正在寻找 Objective-C 中的 category 实现,请务必查看版本 1.x
及更早版本,尽管推荐使用 2.x
。
pod 'AMScrollingNavbar'
use_frameworks!
github "andreamazz/AMScrollingNavbar"
请确保使用 ScrollingNavigationController
来代替标准的 UINavigationController
。您可以直接在 Storyboard 中设置 UINavigationController
的类,或者在代码中以编程方式创建 ScrollingNavigationController
实例。
使用 followScrollView(_: delay:)
来开始跟随可滚动视图(例如: UIScrollView
或 UITableView
)的滚动。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50.0)
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[(ScrollingNavigationController *)self.navigationController followScrollView:self.tableView delay:0 scrollSpeedFactor:1 collapseDirection:NavigationBarCollapseDirectionScrollDown followers:nil];
}
使用 stopFollowingScrollview()
来停止此行为。 请记住在消失时调用此函数。
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.stopFollowingScrollView()
}
}
为了避免重复代码,您可以让您的视图控制器继承 ScrollingNavigationViewController
,它提供了基本的设置实现。 您只需要调用 followScrollView(_: delay:)
即可。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50.0)
}
}
要移动另一个视图,例如工具栏,与导航栏一起移动,您可以将一个或多个视图作为 followers
参数提供。 因为您可能希望追随者向上或向下,所以您必须指定视图开始跟随导航栏后的滚动方向。
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50.0, followers: [NavigationBarFollower(view: customFooter, direction: .scrollDown)])
}
请注意,当从控制器导航离开时,追随者可能会保持滚动偏移量。 请参阅 处理导航 以进行正确的设置。
如果您想进一步滚动导航栏,您可以将可选参数 additionalOffset
用于 followScrollView
调用。
您也可以在 followers
数组中传递 UITabBar
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(tableView, delay: 50.0, followers: [tabBarController.tabBar])
}
您可以设置一个 delegate 来接收导航栏状态更改时的回调。
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.scrollingNavbarDelegate = self
}
Delegate 函数
func scrollingNavigationController(_ controller: ScrollingNavigationController, didChangeState state: NavigationBarState) {
switch state {
case .collapsed:
print("navbar collapsed")
case .expanded:
print("navbar expanded")
case .scrolling:
print("navbar is moving")
}
}
如果带有滚动视图的视图控制器推送新的控制器,您应该在 viewWillDisappear(animated:)
中调用 showNavbar(animated:)
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.showNavbar(animated: true)
}
}
默认情况下,当用户点击状态栏时,可滚动视图会滚动到其内容的顶部。 如果您还想显示导航栏,请确保在您的控制器中包含以下内容
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.showNavbar(animated: true, scrollToTop: true)
}
return true
}
您可以使用可选参数 scrollSpeedFactor
来控制滚动速度
controller.followScrollView(view, delay: 0, scrollSpeedFactor: 2)
查看示例项目以获取更多详细信息。
AMScrollingNavBar 维护其自身的 UINavigationBar 的 tintColor
属性副本。 您需要通过调用 navBarTintUpdated()
来通知 AMScrollingNavBar 色调已更改。
navigationBar.tintColor = UIColor.red
controller.navBarTintUpdated()
查看示例项目以获取更多详细信息。
Andrea Mazzini。 我可以提供自由职业工作,欢迎与我联系。
想支持 这些免费库 的开发吗? 通过 Paypal 给我买杯咖啡 ☕️。
Syo Ikeda 和 每个人 都很友善地提交了 pull request。
The MIT License (MIT)
Copyright (c) 2014-2019 Andrea Mazzini
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.