I'm trying to implement a textField that opens a menu style picker when you tap on it. One of the requirements is to be able to detect when the picker is opened/closed and perform custom actions to update the UI. This doesn't seem to be possible with SwiftUI. I'm wondering if it's possible to do this in Swift and wrap it with UIViewRepresentable for SwiftUI.
What I've have so far in SwiftUI:
@State var options: [String] = ["Option 1", "Option 2", "Option 3"]
@State var selectedOption: String = ""
let placeholder: String = "Placeholder"
Menu {
Picker("Options", selection: selectedOption) {
ForEach(options, id: \.self) { option in
Text(option).tag(option)
}
}
.onChange(of: selectedOption) { newValue in
print("selected:\(newValue)") // <---Could only detect if an option is selected from picker
}
} label: {
HStack {
TextField(placeholder, text: $selectedOption)
.disabled(true)
Image(systemName: "chevron.down")
}
.padding(.horizontal, 16.0)
.frame(height: 44.0)
}
What the UI is supposed to look like:
When the textfield is clicked:
When commenting out Picker, the menu style picker is greyed out: