导航栏色调不透明度不会改变颜色

时间:2021-01-02 20:33:04

I'm trying to make my navigation bar transparent in my app, so I tried to change the bar tint's opacity down to 0 so it's completely clear. However, this has absolutely no effect on the color of the background.

我试图让我的导航栏在我的应用程序中透明,所以我试图将条纹色调的不透明度降低到0,所以它完全清楚。但是,这对背景的颜色完全没有影响。

I've also tried programmatic solutions, putting the following code in viewDidLoad:

我也尝试过程序化解决方案,将以下代码放在viewDidLoad中:

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

However, I've gotten nothing to work. Is there a way that I'm missing to make my navigation bar completely transparent (but keep the bar buttons not transparent)

但是,我没有任何工作要做。有没有一种方法可以让我的导航栏完全透明(但保持条形按钮不透明)

2 个解决方案

#1


1  

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

#2


0  

You must walk through the view hierarchy and find the views that you need to hide. Click on the "Debug View Hierarchy" button to see a list of views and their names.

您必须遍历视图层次结构并找到需要隐藏的视图。单击“调试视图层次结构”按钮以查看视图列表及其名称。

Here's a function that works for me:

这是一个适合我的功能:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    transparentBackgrounds(navigationController?.navigationBar)
}
func transparentBackgrounds(_ view: UIView?) {
    guard let view = view else { return }

    let className = String(describing: type(of: view))
    print(className)
    if ["_UIBarBackground","UIImageView","UIVisualEffectView"].contains(className) {
        view.isHidden = true
    }

    view.backgroundColor = UIColor.clear
    for v in view.subviews {
        transparentBackgrounds(v)
    }
}

#1


1  

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

#2


0  

You must walk through the view hierarchy and find the views that you need to hide. Click on the "Debug View Hierarchy" button to see a list of views and their names.

您必须遍历视图层次结构并找到需要隐藏的视图。单击“调试视图层次结构”按钮以查看视图列表及其名称。

Here's a function that works for me:

这是一个适合我的功能:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    transparentBackgrounds(navigationController?.navigationBar)
}
func transparentBackgrounds(_ view: UIView?) {
    guard let view = view else { return }

    let className = String(describing: type(of: view))
    print(className)
    if ["_UIBarBackground","UIImageView","UIVisualEffectView"].contains(className) {
        view.isHidden = true
    }

    view.backgroundColor = UIColor.clear
    for v in view.subviews {
        transparentBackgrounds(v)
    }
}