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

我只想在一个ViewController中使NavigationBar透明。 但是,在单个ViewController更改NavigationBar后,整个navigationController将变为透明并在几秒钟后崩溃。以下是我的代码块:

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

    }

它崩溃了

self.navigationController!.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)

尝试给定的代码使导航栏在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()

希望这段代码能帮助你..谢谢


viewWillAppear

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

并在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

希望这会帮助:)


我们可以像这样达到这个要求:

UIViewController我们要清除NavigationBar Color应该清楚,在UIViewController我们需要在viewDidLoadviewWillAppearviewWillDisappear方法中编写这些代码

viewDidLoad方法中,如果我们没有写入代码片段,那么我们需要编写这个方法,以便更好地显示结果,然后navigation bar颜色将在显示后进行更改。

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
}

当我们移动到其他屏幕(推另一个UIViewController )在同一个UINavigationController我们需要设置barTintColor否则它将显示为黑色。

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

上一篇: How can I make UINavigationController transparent in one view controller only?

下一篇: Sonarqube Webapp did not start