How can I make UINavigationController transparent in one view controller only?
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 :)
Related videos on Youtube
Isha Balla
Updated on June 26, 2022Comments
-
Isha Balla almost 2 years
I want to make the
NavigationBar
transparent in only oneViewController
. However, upon changing theNavigationBar
in a singleViewController
, the entirenavigationController
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 almost 8 yearsyou can make custom navigation controller for that particular view controller
-
-
Sami over 7 yearsRecommend putting in
viewWillDisappear
instead to avoid the slight delay. Agree with approach though -
Ketan Parmar over 7 yearsDone ! Changed didaopear to willappear
-
AnBisw almost 7 yearsa small problem though, setting the backgroundColor to clear makes the navbar black.
-
Rool Paap about 5 yearsYou got some forced unwraps in your solution, but your solution works for me.
-
Mark almost 4 yearsWorks for Swift 5.x. Wrap it in a UINavigationController extension to make it easier to call