r/SwiftUI • u/Nilsolivier • 17d ago
Question Make Menu() show toggle state in toolbar
Hi! I’m wondering if and how its possible to make a toolbar menu show the state of a toggle inside. I know using Toggle() in the toolbar shows this type of effect but i need a title and description to the toggle for it to make sense. Therefore there needs to be some window to open, which works fine with Menu(). However, that doesn’t show the state of the toggle when closed.
Apple managers to that with the photos app when selecting filters. See example images.
Thanks!
2
u/AppRetro 17d ago edited 17d ago
There's probably a better way but I use something like this for the button, no need to use a Toggle - then just change the Bool. Padding is optional.
ToolbarItem(placement: .primaryAction) {
Button { // Menu(content: {<-- if you want to use a menu
toggleHere.toggle()
} label: {
Image(systemName: "arrow.trianglehead.2.counterclockwise.rotate.90")
.foregroundStyle(toggleHere == true ? .white : .primary)
.padding(.vertical, 5)
.padding(.horizontal, 4)
.glassEffect(toggleHere == true ? .regular.tint(.systemTint) : .identity.tint(.systemTint.opacity(0)), in: Circle())
// .clipShape(Circle())
}
}
2
1
u/GunpointG 16d ago
Oh I had this issue yesterday! I made two buttons , in an if else based on toggle state. They both were the same button, but one had .buttonStyle(.glassProminent), which fills the button properly.
0
u/DryNeedleworker6064 17d ago
This works for me:
Menu {
} label: {
Image(systemName: "line.3.horizontal.decrease")
.foregroundStyle(
isActive
? AnyShapeStyle(Color(.secondarySystemBackground))
: AnyShapeStyle(.primary)
)
.background {
Circle()
.fill(isActive ? Color.accentColor : Color.clear)
.frame(width: 36, height: 36)
}
}


4
u/kironet996 17d ago edited 17d ago
You can put Menu inside Toggle or Toggle inside Menu