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)

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


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


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.

链接地址: http://www.djcxy.com/p/91812.html

上一篇: Rails使用Symbol vs String作为params散列中的键

下一篇: 我如何才能使UINavigationController在一个视图控制器中仅透明?