UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to conditionally change ToolbarItem color?

Forums > SwiftUI

With the following code, I can conditionally assign different colors to navigationTitle text.

init() {
    //Use this color when UINavigationBar shows large title (unscrolled)
    UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.blue]

    //Use this color when UINavigationBar shows inline title (scrolled down
    UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.green]
}

And I wanted to do the same for toolbar items, but couldn't figure out how.

I can set the "general" color of toolbar items like below, but I'm having trouble conditionally assigning different colors to toolbar items depending on if the NavigationBar is in largeTitle mode or inline title mode. Any leads would be helpful, thank you!

ToolbarItem(placement: .navigationBarTrailing) {
    HStack(spacing: 20) {
        Button {
          // button action
        } label: {
            Image(systemName: "location.circle.fill")
                .foregroundColor(.green)
        }
        Button {
            // button action
        } label: {
            Image(systemName: "gear")
                .foregroundColor(.green)
        }
    }
}

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.