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

I want to make the NavigationBar transparent in only one ViewController . However, when you change the NavigationBar in one ViewController entire navigationController becomes transparent even after several seconds of failure. 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 } 

Line failure

 self.navigationController!.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default) 
+7
ios swift uinavigationcontroller uinavigationbar
source share
5 answers

Try entering the code to make the navigation bar transparent in quick: -

  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 helps you. thanks

+2
source share

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 helps :)

+1
source share

As already mentioned, create a custom navigation controller. Here is a tutorial on how to do this (this is in Objc, but I think it shouldn't be a problem for you to do it in Swift)

0
source share

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 
0
source share

We can achieve this requirement as follows:

In which UIViewController we want to clear the navigation bar. The color should be clear in this UIViewController , we need to write this code in viewDidLoad , viewWillAppear and viewWillDisappear method

In the viewDidLoad method viewDidLoad we need to write that for a better result, if we did not write, place a code fragment, then the color of the navigation bar will change after viewing.

 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 switch to another screen (puch another UIViewController ) in the same UINavigationController , we need to set barTintColor , otherwise it will appear as black.

0
source share

All Articles