接受的答案对我有用,但是当我弹出或向前推到另一个vc时,当我希望阴影图像重新出现时,我注意到导航栏中出现了明显的闪烁。
navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
在viewWillAppear中使用此方法,阴影栏被隐藏在当前可见的视图控制器中。
使用这两种方法
navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
navigationController?.navigationBar.setValue(false, forKey: "hidesShadow")
在viewWillDisappear中,闪烁仍然发生,但仅在重新出现阴影图像而不是导航栏本身时才发生。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// 1. hide the shadow image in the current view controller you want it hidden in
navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
navigationController?.navigationBar.layoutIfNeeded()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
// 2. show the shadow image when pushing or popping in the next view controller. Only the shadow image will blink
navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
navigationController?.navigationBar.setValue(false, forKey: "hidesShadow")
navigationController?.navigationBar.layoutIfNeeded()
}