【问题标题】:navigationBar color with alpha value带有 alpha 值的导航栏颜色
【发布时间】:2021-07-16 17:41:38
【问题描述】:

我想在向上滚动时更改导航栏的颜色。我的scrollViewDidScroll 看起来像:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let safeArea: CGFloat = UIApplication.shared.windows.filter{$0.isKeyWindow}.first?.safeAreaInsets.top ?? 0
    let alpha: CGFloat = ((scrollView.contentOffset.y + safeArea) / safeArea)
    // This label becomes visible when scrolled up
    navTitleLabel.alpha = alpha
    self.navigationController?.navigationBar.barTintColor = .yellow.withAlphaComponent(alpha)
}

我什至尝试将 0 硬编码为 .yellow.withAlphaComponent(alpha)。但颜色仍然可见。如果您想知道alpha 的初始值(未滚动时),它是-0.9。如何让导航栏在用户滚动时慢慢可见,例如navBarLabel

这是该行为的 youtube 链接:https://youtu.be/75BjVK-nz4c

【问题讨论】:

    标签: swift uicollectionview


    【解决方案1】:

    您可以通过从您使用的黄色生成图像来做到这一点。然后在 scrollView didScroll 上将导航背景图像设置为您生成的图像。

    extension UIColor {
        func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
            return UIGraphicsImageRenderer(size: size).image { rendererContext in
                self.setFill()
                rendererContext.fill(CGRect(origin: .zero, size: size))
            }
        }
    }
    
    navigationController?.navigationBar.setBackgroundImage(UIColor.orange.withAlphaComponent(alpha).image(),
         for: .default)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-04
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多