Make a transparent navigation bar on push and restore it on pop
So after some digging and some pretty useful hints from @Paulo I have managed to solve this as I wanted to.
This is something that should be way more simple to achieve, and Apple should give developers that simplicity option and not tweaking around some hack to achieve it, but anyway.
I found that one of the secret was that I was abusing the navigationBar.isTranslucent = true / false
when navigating through the view controllers.
In order to do this I set the default navigationBar
properties in the parentViewController
, the one that will push to the view controller with the transparent navigationBar
; I've done it as the following:
self.navigationController?.navigationBar.backgroundColor = .white
self.navigationController?.navigationBar.barTintColor = .white
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
On the pushedViewController
viewWillAppear(_:)
you need to implement the following:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard self.navigationController?.topViewController === self else { return }
self.transitionCoordinator?.animate(alongsideTransition: { [weak self](context) in
self?.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self?.navigationController?.navigationBar.shadowImage = UIImage()
self?.navigationController?.navigationBar.backgroundColor = .clear
self?.navigationController?.navigationBar.barTintColor = .clear
}, completion: nil)
}
Here I set the desired navigationBar
transparency, but as you notice, no need to use the isTranslucent
property, I noticed by forcing it the UI would show some flickering and weird layout on the push animation.
Then on the same view controller (pushed) you need to implement the default, desired, navigationBar
properties that you've implemented in the parentViewController
:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.transitionCoordinator?.animate(alongsideTransition: { [weak self](context) in
self?.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
self?.navigationController?.navigationBar.shadowImage = nil
self?.navigationController?.navigationBar.backgroundColor = .white
self?.navigationController?.navigationBar.barTintColor = .white
}, completion: nil)
}
And by doing this everything should work as expected.
Hope it helps someone in the future.