NavigationBarBackgroundHider

辅助工具,用于在滚动时轻松隐藏导航栏背景

这是什么?

有时,您希望隐藏导航栏的背景,直到用户滚动到特定位置。 例如,您可能有一个标题图像,您希望它不被模糊遮挡,直到用户滚动过去。 这个小库可以很容易地实现这一点。

如何使用?

在您的视图控制器中,调用 enableNavigationBarBackgroundHiding() 以激活导航栏隐藏功能

override func viewDidLoad() {
	// this will hide the navigation bar's background until
	// the user scrolls to the first section of the tableview
	enableNavigationBarBackgroundHiding()
}

如果您没有 `UITableView`,您可以提供自己的偏移量提供程序,告知 bar hider 何时隐藏/显示 bar:只需返回 bar 应该变为可见的位置即可

override func viewDidLoad() {
	// this will make the bar's background visible when the scroll view contentOffset > 100 or 200 
	// depending on some variable. You can put any logic here, just make sure to
	// not retain `self` strongly, otherwise you have a retain cycle.
	enableNavigationBarBackgroundHiding() { [weak self] scrollView in 
		return self?.someVariable == true ? 100 : 200
	}
}

用法

在您的 UIViewController 子类中,您可以选择性地覆盖以下内容

要启用 bar 隐藏,请调用:- enableNavigationBarBackgroundHiding() - 或者,直接指定要使用的滚动视图:enableNavigationBarBackgroundHiding(with: someScrollView)

您还可以传递一个 offset 提供程序回调,用于确定何时显示/隐藏 bar

enableNavigationBarBackgroundHiding(with: someScrollView) {
	return scrollView in return scrollView.contentSize.height * 0.5
}

禁用 bar 隐藏,请调用:- disableNavigationbarBackgroundHiding()

要获取活动的 bar hider,请在您的视图控制器中调用:- self.navigationBarBackgroundHider

自定义

Bar hider 有很多自定义点。 首先,在启用它之后,调用 navigationBarBackgroundHider 以获取当前的 bar hider。

所有处理程序都有默认实现。 默认实现也是可访问的,可以直接调用。 您可以使用它来动态覆盖某些行为:- defaultUpdateHandler(在需要时调用 makeNavigationBarBackgroundTransparentHandlermakeNavigationBarBackgroundVisibleHandler)- defaultMakeBackgroundTransparentHandler(调用 UINavigationItem.makeNavigationBarBackgroundTransparent())- defaultMakeBackgroundVisibleHandler(调用 UINavigationItem.makeNavigationBarBackgroundVisible())- defaultOffsetForHidingProvider(返回 UITableView 的第一个节的偏移量,否则返回 0)