我试着在Xcode12.5.1中解决这个问题,这样我就可以转移到Xcode13。我已经在Xcode13中运行了这个应用程序,这就是我发现问题的原因。我知道我来晚了,但在Xcode13…之前它没有坏掉。
在我的应用程序中,我有三种配置的自定义主题设置,夏天,秋天和冬天。每个主题设置navBar的颜色属性以及应用程序中其他控件的颜色属性。主题是在设置VC上从分段控件中选择的。
setNavbarAttributes()代码在didFinishLaunchingWithOptions中从AppDelegate运行。它在应用程序启动时设置正确的navBar属性。在这种情况下,代码按预期工作。我可以通过导航到应用程序中的其他视图并观察navBar显示正确的颜色属性来验证这一点。
当从设置VC上的分段控件中选择一个主题时,setNavbarAttributes()代码也会运行。这是问题所在。当主题改变时,navBar颜色属性不会传递到其他视图中的navBar。有人知道为什么这个不起作用吗?我有一个不太好的变通办法,把VC的设置更新代码放在一个扩展中,但这意味着要接触到每一个VC。这看起来不太对。
它在下面显示的旧代码中工作得很好,但在使用UINavigationBarAppearance()的Xcode13中就不行了。
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)]这将在整个应用程序中设置navBar的属性。
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
}
}此代码在viewDidLoad上的设置VC中运行,并在点击主题选择器时运行。
// 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
}https://stackoverflow.com/questions/69420879
复制相似问题