I have a Picker with pickerStyle set to MenuPickerStyle
When nothing is selected, the Picker label should prompt to select something.
When something is selected, the Picker label should show text related to the selected item.
When I select an item, the label does display "what" I want, but it is faded/dim. When I select the SAME item again, the label shows dark as you would expect.
It is almost as though the menu is still showing, but it's not. Other items on the view are fully functional as well.
My application is more complex as the Picker is populated by core data records, but this code below shows the issue. The button simply posts an alert showing that focus is not the issue.
import SwiftUI
struct SelectedItemView: View, Identifiable {
let id = UUID()
@State var item: String
var body: some View {
HStack {
Text(self.displayText)
.font(.headline)
}
}
var displayText: String {
"Core Data Item: \(item)"
}
}
struct TestPickerView: View {
@State var showAlert: Bool = false
@State var selectedIndex: Int = -1
let items: [String] = [
"One",
"Two",
"Three",
"Four",
"Five"
]
var body: some View {
VStack {
Text("Select an Item...")
Picker(selection: $selectedIndex, label: Text(self.selectedPickerLabelText)) {
ForEach(0 ..< self.items.count) {
SelectedItemView(item: self.items[$0])
}
}
.padding()
.pickerStyle(MenuPickerStyle())
.onReceive([self.selectedIndex].publisher.first()) { value in
self.itemDidChange(value)
}
Button(action: {
self.showAlert = true
}) {
Text("Show Selected Item")
}.padding()
.border(Color.blue)
}.alert(isPresented: $showAlert) {
Alert(title: Text("Item Selected"),
message: Text(selectedPickerLabelText))
}
}
func itemDidChange(_ value: Int) {
print("Do some stuff...")
}
var selectedPickerLabelText: String {
if selectedIndex < 0 {
return "Choose something..."
}
return "You selected: \(items[selectedIndex])"
}
}
struct TestPickerView_Previews: PreviewProvider {
static var previews: some View {
TestPickerView()
}
}