LFHS
LFHS

Reputation: 305

Navigation Bar tint opacity doesn't change the color

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.

I've also tried programmatic solutions, putting the following code in 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)

Upvotes: 0

Views: 406

Answers (2)

Okan
Okan

Reputation: 410

    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

Upvotes: 1

Matt H
Matt H

Reputation: 6540

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)
    }
}

Upvotes: 1

Related Questions