Swift - 在 iOS 中使导航栏透明的问题

Swift - Issue in making the navigation bar transparent in iOS

我在使 NavigationBar 透明而不是显示白色作为背景色时遇到问题。

我想实现这个

但是我得到了以下 NavigationBar 作为背景颜色。

它显示白色作为背景色。它没有变透明

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear

您也试试下面的代码。

  self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default);
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
    self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(imageWithColor(color: color.withAlphaComponent(alpha)), for: .default)
self.navigationController?.navigationBar.shadowImage = imageWithColor(color: color.withAlphaComponent(alpha))
fileprivate func imageWithColor(color : UIColor) -> UIImage? {
    let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
    UIGraphicsBeginImageContext(rect.size)
    let ctx = UIGraphicsGetCurrentContext()
    ctx?.setFillColor(color.cgColor)
    ctx?.fill(rect)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

打电话给 func viewWillAppera。你会得到