[英]Change NavBarTitle on SystemColor change
Good day everyone,今天是个好日子,
I was trying to change the titleTextAttributes
of the UINavigationBar.appearance()
depending on the actual SystemColor (.dark, .light)我试图根据实际的 SystemColor (.dark, .light) 更改UINavigationBar.appearance()
的titleTextAttributes
First of all I used an initializer to change the .foregroundColor
, everything works fine.首先,我使用初始化程序来更改.foregroundColor
,一切正常。 Additionally I want to automatically get it changed depending on light- or darkmode using colorScheme
.此外,我想根据使用colorScheme
的亮模式或暗模式自动更改它。 This works fine on a button I added into the NavBar:这适用于我添加到导航栏的按钮:
ToolbarItem(placement: .navigationBarTrailing) {
Button {
showingAddView.toggle()
} label: {
Label("Hinzufügen", systemImage: "plus.circle").foregroundColor(colorScheme == .dark ? Color.white : Color.black)
}
Now I have the problem not knowing how to solve that for UI Components... I tried integrating colorScheme
like this:现在我有一个问题,不知道如何为 UI 组件解决这个问题......我尝试像这样集成colorScheme
:
init() {
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.largeTitleTextAttributes = colorScheme == .dark ? [.foregroundColor: UIColor.white] : [.foregroundColor: UIColor.black]
navBarAppearance.titleTextAttributes = colorScheme == .dark ? [.foregroundColor: UIColor.white] : [.foregroundColor: UIColor.black]
}
But it didn't work但它没有用
Does anybody know how to use colorScheme
or any other modifier to achieve this?有谁知道如何使用colorScheme
或任何其他修饰符来实现这一点? Thank you all in advance!谢谢大家!
Greetings Marcel问候马塞尔
@Environment(\.colorScheme) var colorScheme
Does this help you?这对你有帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.