How can I make UINavigationController transparent in one view controller only?

10,654

Solution 1

We can achieve this requirement like this :

In Which UIViewController we want to clear the NavigationBar Color should be clear in that UIViewController we need to write these code in viewDidLoad, viewWillAppear and viewWillDisappear method

In viewDidLoad method we need to write that for better appear result if we did not write put the code snippet then the navigation bar color will change after will view shown.

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.navigationBar.barTintColor = UIColor.clear
    self.navigationController?.navigationBar.backgroundColor = UIColor.clear
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
    self.navigationController?.navigationBar.shadowImage = nil
    self.navigationController?.navigationBar.isTranslucent = true
}

When we move to other screen(push another UIViewController) on the same UINavigationController we need to set the barTintColor otherwise it will be appear as black color.

Solution 2

Try given code to make navigation bar transparent in swift :-

    self.navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.translucent = true
    self.navigationController!.view.backgroundColor = UIColor.clearColor()
    self.navigationController?.navigationBar.backgroundColor = UIColor.clearColor()

Hope this code will help you.. Thanks

Solution 3

Swift 4

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.backgroundColor = UIColor.clear

Solution 4

In viewWillAppear,

  self.navigationController!.navigationBar.backgroundColor = UIColor.clearColor()

and in viewWillDisappear

  self.navigationController!.navigationBar.backgroundColor = UIColor(red: (247.0 / 255.0), green: (247.0 / 255.0), blue: (247.0 / 255.0), alpha: 1)  // this is default bar color you can set your desired color if you are using custom color for navigation bar

Hope this will help :)

Share:
10,654

Related videos on Youtube

Isha Balla
Author by

Isha Balla

Updated on June 26, 2022

Comments

  • Isha Balla
    Isha Balla almost 2 years

    I want to make the NavigationBar transparent in only one ViewController. However, upon changing the NavigationBar in a single ViewController, the entire navigationController becomes transparent and after a few second crashes.Here is my block of code:

    override func viewWillAppear(animated: Bool) {
            self.navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
            self.navigationController?.navigationBar.shadowImage = UIImage()
            self.navigationController?.navigationBar.translucent = true
            self.navigationController!.view.backgroundColor = UIColor.clearColor()
        }
    
    
    
    override func viewDidDisappear(animated: Bool) {
            self.navigationController!.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)
            self.navigationController?.navigationBar.shadowImage = nil
            self.navigationController?.navigationBar.translucent = true
    
        }
    

    It crashes in line

    self.navigationController!.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)
    
    • Jagveer Singh
      Jagveer Singh almost 8 years
      you can make custom navigation controller for that particular view controller
  • Sami
    Sami over 7 years
    Recommend putting in viewWillDisappear instead to avoid the slight delay. Agree with approach though
  • Ketan Parmar
    Ketan Parmar over 7 years
    Done ! Changed didaopear to willappear
  • AnBisw
    AnBisw almost 7 years
    a small problem though, setting the backgroundColor to clear makes the navbar black.
  • Rool Paap
    Rool Paap about 5 years
    You got some forced unwraps in your solution, but your solution works for me.
  • Mark
    Mark almost 4 years
    Works for Swift 5.x. Wrap it in a UINavigationController extension to make it easier to call