Learn Courses My Dashboard

iOS Foundations (SwiftUI) M4L9 challenge MenuPickerStyle()

Hello!

What can be the problem? MenuPickerStyle() doesn’t show the label “Location”, it shows the chosen location instead.

Here is the code:

HStack(spacing: 30) {
                
                Picker("Location", selection: $locationPickerIndex) {
                    
                    ForEach(0..<locations.count) { i in
                        Text(locations[i]).tag(i)
                    }
                    
                }
                .pickerStyle(MenuPickerStyle())
                
                Text(locations[locationPickerIndex])
                
            }

And here is the output of the simulator:

SegmentedPickerStyle() label works well.

What you are seeing is correct since you have chosen the picker style to be MenuPickerStyle(). The label is suppressed.

But in M4L9 SwiftUI Picker the MenuStylePicker shows the label “Tap Me” not the “Option 1…3”.

What is the difference here?

Hmmmmm, I suspect that this might have something to do with changes from Xcode 12 to Xcode 13 and moving from iOS 14 to iOS 15.

If I run the solution provided in the Course Resources I see exactly what you see and that’s what I also see in my own version of the challenge.

Hi, did anyone find a solution to this problem? How to keep the “location” word displayed and only have the city change?

Thanks

I did a bit of a workaround by keeping my text label static as “Location” and making the picker as the label which shows the city

HStack {
                
                Text("Location:")
                
                
                Picker("Location", selection: $locationTag, content: {
                    
                    Text("Deira").tag(0)
                    Text("Nahda").tag(1)
                    Text("Tawar").tag(2)
                    Text("BurDubai").tag(3)
                    Text("Mamzar").tag(4)
                    Text("Muhaisna").tag(5)
                    
                    
                }).pickerStyle(MenuPickerStyle())
                
            }
1 Like