Search code examples
swiftuikituinavigationbar

Navigation Bar will not become transparent


I need the navigation bar to become transparent. Here is what i have put in my ViewController:

class ViewController: UIViewController {

  @IBOutlet weak var navbar: UINavigationBar!

  override func viewDidLoad() {
    super.viewDidLoad()
    navbar.isTranslucent=true
    navbar.shadowImage = UIImage()
    navbar.backgroundColor = UIColor.clear
  } 

According to debug view hierarchy UIVisualEffectBackdropView, UIVisualEffectSubview and UIVisualEffectSubview(yes two of them) are preventing it from becoming transparent. Ps: I am not using a Navigation Controller.

How can i fix it?


Solution

  • This three lines should be enough to make your UINavigationBar transparent.

    override func viewDidLoad() {
        super.viewDidLoad()
        // make transparent the navbar
        navbar.setBackgroundImage(UIImage(), for: .default)
        navbar.shadowImage = UIImage()
        navbar.isTranslucent = true
    }