Post

Replies

Boosts

Views

Activity

Reply to Picker Label not showing anymore
Using the Picker in a Form renders the label as @Merlin52 pointed out. The Form really takes over your design though, which is nice but not when you need to customize it. Here's another sample with a screenshot: struct ContentView: View { var fruits = ["Banana","Apple", "Peach", "Watermelon", "Grapes" ] @State private var selectedFruit = 0 var body: some View { Form { // Variation 1 Picker(selection: $selectedFruit, label: Text("Select Favorite Fruit")) { ForEach(0..<fruits.count, id: \.self) { Text(self.fruits[$0]) } } // Variation 2 Picker(selection: $selectedFruit) { ForEach(0..<fruits.count, id: \.self) { Text(self.fruits[$0]) } } label: { HStack { Text("Favorite Fruit") Divider() Text(fruits[selectedFruit]) } } // Variation 3 Menu { ForEach(0..<fruits.count, id: \.self) { Text(self.fruits[$0]) } } label: { HStack { Text("Favorite Fruit") Divider() Text(fruits[selectedFruit]) } } } .pickerStyle(.menu) } } Here's how everything looks like when I nest everything in a VStack instead of a Form: Notice I added a Menu in there, which seems like the behaviour the OP is expecting. Shouldn't the Picker mimic that behaviour when modified with a MenuPickerStyle? This would mean the label parameter could be optional in the Picker initializer (so it can default to displaying the selected row). Repurposing a Menu as a picker is the painful alternative in the meantime.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’22
Reply to Picker Label not showing anymore
If you embed the Picker inside of a Menu, it lets you create a custom label. I'm not sure if this was intended by SwiftUI, but it seems to work perfectly as expected. Menu { Picker(selection: $selectedFruit, label: Text("Select Favorite Fruit")) { ForEach(0..<fruits.count, id: \.self) { Text(fruits[$0]) } } } label: { HStack { Text("Favorite Fruit") Divider() Text(fruits[selectedFruit]) } } Source: https://stackoverflow.com/q/70835085
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’22