带背景的自定义导航栏

Custom Navigation Bar With Background

由于我是 Swift 的新手并且正在研究我的 FYP,我有多个 VC,现在我将在下面的附件 VC 中找到.pushViewController(nextVC, animated: true)。我正在尝试使导航栏透明,以便背景图像显示在导航栏后面:

目前我的导航栏看起来像:

这是我的代码,

override func viewDidLoad() {
    super.viewDidLoad()

    //------
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
}

希望你们能理解我的问题。任何帮助将不胜感激。 谢谢

经过长时间的斗争,我找到了问题的解决方案。你可以说这很棘手,但可能对你有帮助 Xcode 9 及更高版本和 swift 4.

关于我的问题,我将通过 pushViewController() 进入这个 VC,所以当我到达那个 VC 时,我只是在这里使用 ViewWillAppear() 来改变我的 NavBar 并从这个 VC 移回之前的 VC 或任何其他控制器,因此我需要重置原始导航以恢复其原始样式,为此我正在使用 viewWillDisappear().

第 1 步:

override func viewWillAppear(_ animated: Bool) {

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true

}
override func viewWillDisappear(_ animated: Bool) {

    super.viewWillDisappear(animated)
    self.navigationController?.navigationBar.shadowImage = nil
    self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
    self.navigationController?.navigationBar.isTranslucent = true

}

第 2 步:

为了增加导航栏的高度,我在导航项的提示中添加了空格,并将 y 坐标设置为 -20 以将 NavBar 到达 Storyboard 导航栏下方状态栏的顶部。

第 3 步:最后也是最后的是

我已经将 Storyboard 导航栏下的 Alpha 值设置为 0.5

这就是我的全部,并且非常适合我。您也可以在完成上述所有步骤后看到我的输出。

enter image description here