swift - 如何使用滚动更改导航栏和 BarButtonItem 的颜色
问题描述
我想在向下滚动 TableView 时平滑地更改 UINavigationBar 和此 NavigationBar 内的 UIBarButtonItem 的背景颜色。我在 AirBNB 应用程序中看到了这一点,但不知道如何做这样的事情。谢谢你的帮助
解决方案
- 符合
UIScrollViewDelegate
并确保您的 tableView/CollectionView 委托设置为当前 ViewController。 - 实现
scrollViewDidScroll(:)
方法。 - 决定您首选的偏移量以更改 NavigationBarColor。
- 如果满足偏移条件,则更改 NavigationBarColor。
概括
Class UIViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.y
if offset > 1 { // Or choose any desired offset
// TODO:- Change your color for offset greater than 1
}else {
// TODO: - Change your nav bar for offset less than 1
}
}
}
推荐阅读
- css - 我的根组件的样式未应用于我的 Angular 应用程序
- google-maps - 拖动后由标记响应的坐标不正确
- msbuild - 如何修复 MsBuild 错误 - MSB6006: "CL.exe" exited with code -1073741510
- erlang - 自动连接到 erlang 集群
- go - 为什么在 Golang 中解组 DER ASN.1 大整数仅限于 SEQUENCE?
- google-signin - Android 应用程序被“邮件想要:离线访问”阻止
- aframe - 使用 Raycaster 获得颜色
- typo3 - TYPO 9.5.x 中没有页面段的口语 URL 的 Extbase routeEnhancers
- c# - 切换平台后unity3d没有响应
- neo4j - 只要有行就重复 neo4j 密码查询