问题
I want to use the new Menu
with SwiftUI but unfortunately there is one issue which I can't solve. After selecting one value of the Menu
the Menutitle looks like so:
However I don't want the title to decrease it's opacity. How can I achieve this?
Here is my code:
struct ContentView: View {
@State private var selectionVariable = 0
let sampleDict = [0: "Sample Title 1", 1: "Sample Title 2"]
var body: some View {
Menu {
Picker(selection: $selectionVariable, label: Text("")) {
ForEach(sampleDict.sorted(by: <), id: \.key) { base, name in
Text(name)
}
}
}
label: {
Text("Eingaben im \(sampleDict[selectionVariable] ?? "")")
}
}
}
回答1:
I'd say it looks like a bug, anyway worth submitting feedback to Apple.
Here is tested workaround (Xcode 12.1 / iOS 14.1)
label: {
Text("Eingaben im \(sampleDict[selectionVariable] ?? "")")
.id(selectionVariable) // << this one !!
}
回答2:
It looks like a bug.
A possible workaround is to remove Picker
animations using .animation(nil)
:
struct ContentView: View {
@State private var selectionVariable = 0
let sampleDict = [0: "Sample Title 1", 1: "Sample Title 2"]
var body: some View {
Menu {
Picker(selection: $selectionVariable, label: Text("")) {
ForEach(sampleDict.sorted(by: <), id: \.key) { base, name in
Text(name)
}
}
}
label: {
Text("Eingaben im \(sampleDict[selectionVariable] ?? "")")
}
.animation(nil) // add here
}
}
来源:https://stackoverflow.com/questions/64770871/issue-with-menu-title-and-decreased-opacity