如何使用滚动更改导航栏和 UIBarButtonItem 的颜色

How to change color of navigationBar & BarButtonItem with scroll

我想在向下滚动 TableView 的同时更改此 NavigationBar 中的 UINavigationBar 和 UIBarButtonItem 的背景颜色平滑。我在 AirBNB 应用程序中看到了这个,但不知道如何做这样的事情。感谢您的帮助

  1. 符合 UIScrollViewDelegate 并确保您的 tableView/CollectionView 委托设置为当前的 ViewController。
  2. 实施scrollViewDidScroll(:)方法。
  3. 确定您的首选偏移量以更改 NavigationBarColor。
  4. 如果满足偏移条件,则更改 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
       }
   }

}