I’m trying to work this out in Xcode 12.5.1 so I can move to Xcode 13. I have run this app in Xcode 13 Released and that’s how I discovered the problem. I know I’m a little late to the game but before Xcode 13… it wasn’t broken.
In my apps I have custom themes setup in three configurations, summer, fall and winter. Each theme sets the color attributes of the navBar as well as those of the rest of the controls in the app. The theme is chosen on the settings VC from a segmented control.
The setNavbarAttributes() code is run from AppDelegate in didFinishLaunchingWithOptions. It sets the correct navBar attributes when the app launches. In this case the code work as expected. I can verify this by navigating to the other views in the app and observing that the navBar shows the correct color attributes.
The setNavbarAttributes() code also runs when a theme is chosen from the segmented control on the settings VC. Here is the problem. When the theme is changed the navBar color attributes are not carried through to the navBar in the other views. Does anybody know why this isn’t working? I have a not so great workaround by putting the settings VC update code in an extension but that means touching every VC. That doesn't seem right.
It worked fine with my old code shown below but that’s broken in Xcode 13 with UINavigationBarAppearance().
let theNavebarProperties = UINavigationBar.appearance()
theNavebarProperties.barTintColor = Theme.current.navbarColor
theNavebarProperties.isTranslucent = false
theNavebarProperties.titleTextAttributes = [.foregroundColor: Theme.current.accentColor, .font: UIFont.systemFont(ofSize: gNavBarTitleTextSize, weight: .semibold)]
This sets the attributes of the navBar across the app.
class SetNarbar
{
static func setNavbarAttributes()
{
let theAppearance = UINavigationBarAppearance()
theAppearance.configureWithOpaqueBackground()
theAppearance.backgroundColor = Theme.current.navbarColor
theAppearance.titleTextAttributes = [.foregroundColor: Theme.current.accentColor, .font: UIFont.systemFont(ofSize: gNavBarTitleTextSize, weight: .semibold)]
UINavigationBar.appearance().standardAppearance = theAppearance
UINavigationBar.appearance().scrollEdgeAppearance = theAppearance
UINavigationBar.appearance().compactAppearance = theAppearance
}
}
This code is run in the settings VC on viewDidLoad and when the theme selector is tapped.
// Sets the controls on the settings VC to the currently selected theme. I have omitted code that works and does not pertain to setting the navBar.
func updateThemeOnSettingsVC()
{
setNeedsStatusBarAppearanceUpdate()
SetNarbar.setNavbarAttributes()
let navAppearance = UINavigationBarAppearance()
navAppearance.configureWithOpaqueBackground()
navAppearance.backgroundColor = Theme.current.navbarColor
navAppearance.titleTextAttributes = [.foregroundColor: Theme.current.accentColor, .font: UIFont.systemFont(ofSize: gNavBarTitleTextSize, weight: .semibold)]
navigationItem.standardAppearance = navAppearance
navigationItem.scrollEdgeAppearance = navAppearance
navigationItem.compactAppearance = navAppearance
}