Half of a solution: (reason it's half see bottom)
Assuming you have a ToolbarItem
:
ToolbarItem {
Button(
action: toggleSidebar,
label: { Label("Sidebar", systemImage: "sidebar.left") }
)
}
You could add a Bool
value in your view @State private var sidebarOpened: Bool = true
Then you can toggle that value with the same button:
ToolbarItem {
Button(
action: {
toggleSidebar()
sidebarOpened.toggle()
}
label: { Label("Sidebar", systemImage: "sidebar.left") }
)
}
Now you can set the color of your label based on sidebarOpened
:
label: { Label("Sidebar", systemImage: "sidebar.left") }
.foregroundColor(sidebarOpened ? .red : .secondary)
Note:
This method doesn't really know if the sidebar is open or not, instead it works based on if the button was clicked, so it gives you different result if you left the sidebar opened or not the last time you closed the app.
It would be better to have a value of whether the sidebar is open or not, which I don't think there's a native SwfitUI solution yet. Probably need to check AppKit
NSSplitView
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…