转换后iOS statusBar背景变为黑色

时间:2018-06-06 13:05:44

标签: ios swift uinavigationbar

我正在从UINavigationController内的VC1推送到VC​​2。 VC2有一个透明的navigationBar。

在VC2中的viewDidLoad中:

self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.barTintColor = .clear
self.navigationController?.navigationBar.backgroundColor = .clear

当我按下后退按钮时我想恢复导航栏(绿色),所以在viewWillDissapear里面我有以下内容:

self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController?.navigationBar.barTintColor = .green
self.navigationController?.navigationBar.backgroundColor = .green

但是我在过渡期间得到了这个奇怪的黑色状态栏:

strange black line on top enter image description here

Here is a sample project

这是预期的转变: enter image description here

1 个答案:

答案 0 :(得分:1)

你能在下面试试吗,

override func viewDidLoad() {
    super.viewDidLoad()

    UINavigationBar.appearance().backgroundColor = .clear
    UINavigationBar.appearance().barTintColor = .clear

    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    UINavigationBar.appearance().backgroundColor = .green
    UINavigationBar.appearance().barTintColor = .green

    self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
}