How to create appearance of "pressed state" for Toolbar button Item

I am trying to create an appearance of a "pressed button" state for a Toolbar Button Item using SwiftUI ... just like the Calendar App sidebar toolbar button items in iOS 14.

I have tried "inverting" the background and foreground color using a @State bool property - but doesn't seem to have no affect at all.

Any help will be greatly appreciated.

Answered by karma.rules in 629493022
I figured out the actual solution for my needs was quite simple. I was on a detour of creating a custom swiftui segmented control w/ non-momentary toggle buttons (as available in UISegmentedControl). But the actual solution turns out to be so simple that I am embarrassed that I spent the last couple of days on such a complicated solution :)

The actual solution solution was as simple as below...


Code Block swift
                HStack(spacing: 10) {
                    Image(systemName: "tray.and.arrow.down").tag("My Mailbox").onTapGesture {
                        print("tapped My Mailbox")
                    }
                    .padding(5)
                    .foregroundColor(!showMailboxesTab ? Color.white : Color.accentColor)
                    .background(!showMailboxesTab ? Color.accentColor : Color.clear)
                    .clipShape(RoundedRectangle(cornerRadius: 4))
                    .animation(.linear)
                    Image(systemName: "tray.2").tag("Mailboxes").onTapGesture {
                        print("tapped Mailboxes")
                        showMailboxesTab.toggle()
                    }
                    .padding(5)
                    .foregroundColor(showMailboxesTab ? Color.white : Color.accentColor)
                    .background(showMailboxesTab ? Color.accentColor : Color.clear)
                    .clipShape(RoundedRectangle(cornerRadius: 4))
                    .animation(.linear)
                }



Accepted Answer
I figured out the actual solution for my needs was quite simple. I was on a detour of creating a custom swiftui segmented control w/ non-momentary toggle buttons (as available in UISegmentedControl). But the actual solution turns out to be so simple that I am embarrassed that I spent the last couple of days on such a complicated solution :)

The actual solution solution was as simple as below...


Code Block swift
                HStack(spacing: 10) {
                    Image(systemName: "tray.and.arrow.down").tag("My Mailbox").onTapGesture {
                        print("tapped My Mailbox")
                    }
                    .padding(5)
                    .foregroundColor(!showMailboxesTab ? Color.white : Color.accentColor)
                    .background(!showMailboxesTab ? Color.accentColor : Color.clear)
                    .clipShape(RoundedRectangle(cornerRadius: 4))
                    .animation(.linear)
                    Image(systemName: "tray.2").tag("Mailboxes").onTapGesture {
                        print("tapped Mailboxes")
                        showMailboxesTab.toggle()
                    }
                    .padding(5)
                    .foregroundColor(showMailboxesTab ? Color.white : Color.accentColor)
                    .background(showMailboxesTab ? Color.accentColor : Color.clear)
                    .clipShape(RoundedRectangle(cornerRadius: 4))
                    .animation(.linear)
                }



How to create appearance of "pressed state" for Toolbar button Item
 
 
Q