SwiftUI Dynamic List

I am developing an application that allows users to create recipes. The recipes can only contain ingredients from a pre-selected list. I'm looking for something where, when I select a text field, a list pops up and, when I select an item in that list, it will dismiss the list view and populate the text field with the item that was selected. Does something like this exist natively in SwiftUI or in some 3rd party library?

Replies

I would do it differently:

  • create a Label instead of TextField
  • use onTapGesture modifier to display a List view where you will have all the options
  • return the selected item in List
  • change the Label

Of course, that will not let you edit the label, but I understand you don't need it.

You could also use a Picker for the selection:

https://stackoverflow.com/questions/60048058/swiftui-tap-gesture-recogniser-only-called-once-when-it-effects-a-state-change

More on the use of Picker : You could have a Picker that you show and hide on purpose, below the Text:

h t t p s : / / betterprogramming.pub/using-a-picker-in-a-swiftui-list-ff33b2ce979a