I have a ToolbarItem that when tapped, brings a menu of either Fahrenheit or Celsius. Right now I have that if the user taps on "Celsius" the temperatures will change to celsius unit and vice versa. However, I would like to add a check mark when the user selects Fahrenheit or Celsius and have it change when they select it. Here is my code. I've tried utilizing Picker but not sure how to add the action or a button within it.
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Menu {
Button {
isCelsius = false
} label: {
Text("Fahrenheit °F")
}
Button {
isCelsius = true
} label: {
Text("Celsius °C")
}
} label: {
Image(systemName: "list.dash")
}
}//toolbar item
}//toolbar
CodePudding user response:
You can put a Picker
inside the Menu
:
The .tag()
s connect the selection to the state var.
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Menu {
Picker("", selection: $isCelsius) {
Text("Fahrenheit °F").tag(false)
Text("Celsius °C").tag(true)
}
} label: {
Image(systemName: "list.dash")
}
}
}