TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

MenuPickerStyle looks disabled

Forums > SwiftUI

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()
    }
}

2      

I think it's an Xcode bug - if you upgrade to 12.2, it'll work ok.

Ron

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.