Swift 4 : 下一页的导航栏看起来不同

Swift 4 : Navigation Bar looks different for the next page

我刚刚使用以下代码删除了导航栏阴影线:

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

当我使用这段代码并尝试打开属于同一导航控制器的另一个页面时,它的导航栏看起来不一样。我试图设置相同的导航栏背景颜色和色调颜色,但它不起作用。但是,当我删除这些代码时,我使用的所有页面都可以正常工作。我该如何解决这个问题?

截图:

看起来你的导航栏是半透明的,但你的视图控制器没有在它后面延伸,所以你需要 window 默认为黑色。

两者都

  1. 选中此框以在界面生成器
  2. 的顶部栏下扩展 VC
  3. 将 UIApplication.shared.keyWindow?.backgroundColor 更改为 .white
  4. 使您的导航栏不透明

这是常见的行为。当您设置 backgroundImage 时,则无法设置新颜色。您需要将 setBackgroundImage 设置为 nil,然后在 next ViewController 中设置您想要的新颜色。

这个库可以帮你轻松搞定https://github.com/MoZhouqi/KMNavigationBarTransition PS:参见 link

的示例
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configureAppearance()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        configureAppearance()
    }

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


class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configureAppearance()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        configureAppearance()
    }

    func configureAppearance() {
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
        self.navigationController?.navigationBar.barTintColor = UIColor.yellow
    }
}