Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 473
Isha Balla Avatar asked May 18 '16 11:05

Isha Balla


People also ask

How do I make my navigation controller transparent?

You need to do three things to make a navigation bar transparent. Set background image to non-nil empty image ( UIImage() ). Set shadow image to non-nil empty image ( UIImage() ). Set isTranslucent to true .

What is Navigationitem?

The navigation item used to represent the view controller in a parent's navigation bar.

How do I change the navigation bar color in Swift?

navigationController. navigationBar. barTintColor = UIColor. newBlueColor() and of course this just changes the colour of the navigation bar of the view controller that the code is within.


2 Answers

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.

like image 147
Sumeet Mourya Avatar answered Sep 21 '22 06:09

Sumeet Mourya


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

like image 36
Neha Gupta Avatar answered Sep 22 '22 06:09

Neha Gupta