0

Good day everyone,

I was trying to change the titleTextAttributes of the UINavigationBar.appearance() depending on the actual SystemColor (.dark, .light)

First of all I used an initializer to change the .foregroundColor, everything works fine. Additionally I want to automatically get it changed depending on light- or darkmode using 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:

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? Thank you all in advance!

Greetings Marcel

Marcii
  • 39
  • 6

1 Answers1

0
 @Environment(\.colorScheme) var colorScheme

Does this help you?

  • Hey, thanks for your answer! I already integrated that, thats why my button works fine but I can't figure out how to use it on the navigationView title! – Marcii Sep 15 '22 at 06:29