I've discovered a weird behavior on iOS 11 when I push UIViewController
and change the UINavigationBar
color to transparent. Important thing is that I'm using largeTitles
.
I want to change red color of the navigation bar to transparent and this works fine.
However, if I tap on backButton, disable transparent style and red color style again something bad happened. NavigationBar on the ViewController is not red but still transparent.
As @Menoor Ranpura suggest I add another line which sets also a
backgroundColor
of view inUIViewController
- and this is fine workaround when you set the same color like onUINavigationBar
. However, it's not the solution for the problem because the large part of the navigation bar is still transparent. You can see it when you set the different color for a background. For example, I set the yellow. You can see the example here:
Question
How to properly change the navigation bar color from transparent to red again, when the prefersLargeTitles
is set to true
?
Code
class ExampleTableTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseID")
navigationController?.navigationBar.redNavigationBar()
title = "Red NavBar"
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.backgroundColor = .yellow
navigationController?.navigationBar.redNavigationBar()
}
}
//Transparent Navigation bar controller
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "Transparent NavBar"
view.backgroundColor = .blue
self.navigationController?.navigationBar.prefersLargeTitles = true
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.transparentNavigationBar()
}
}
extension UINavigationBar {
func redNavigationBar() {
setBackgroundImage(nil, for: .default)
shadowImage = nil
prefersLargeTitles = true
tintColor = .white
largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
barTintColor = .red
}
func transparentNavigationBar() {
setBackgroundImage(UIImage(), for: .default)
shadowImage = UIImage()
}
}
Some tips that I've already tested:
- Everything works fine when
prefersLargeTitles
is set tofalse
- Everything works fine when
prefersLargeTitles
is set totrue
, butnavigationBar
changes are between non transparent colors. For example, if changing between green <-> yellow - I don't want to set
backgroundColor
on view. It's not an solution but kind of a workaround for this.
Here you can see a screen from XCode:
Interesting fact is that, there is something called: _UINavigationBarLargeTitleView
which is transparent. How to access it?
Related problems:
You can find example project here: https://github.com/kamwysoc/LargeTitlesNavigationBarTransition
UPDATE referring to @Menoor Ranpura answer
Code that @Menoor Ranpura suggest is a kind of a workaround. It's not a solution to set the same backgroundColor
on UIViewController
view like UINavigationBar
has. However, I go a bit further and I change the color for a different than UINavigationBar
controller has. And as you can see on above gif, when the largeTitle
appears, the navigation bar becomes yellow - which means that is transparent - because we're able to see the yellow background of view.