1

I am currently learning Swift and have encountered a problem with SwiftUI for which I cannot find a solution. When I create a picker with text and an image, for example, I am unable to format them. I have also tried using resizable() but it still doesn't work. Additionally, the text cannot be edited. Is this intentional or am I just doing something wrong? (The PickerStyle is Menu)

import SwiftUI

struct LanguagePicker: View {
    @Binding var selection: Country
    
    var body: some View {
        Picker("Sprache", selection: $selection) {
            ForEach(Country.allCases) { country in
                HStack {
                    Text("  \(country.language)")
                    country.image
                        .resizable()
                        .frame(maxWidth: 10, maxHeight: 10)
                }
                .tag(country)
            }
        }
        .font(.title2)
        .pickerStyle(.menu)
        .tint(.black)
    }
}

#Preview {
    LanguagePicker(selection: .constant(.japan))
}

PickerView

When I click on the image, the picker works with the desired sizes:

Screenshot

2
  • 1
    The picker is very limited in what formatting you can do to the content. It looks like this is intentional from Apple. Commented Apr 6, 2024 at 6:17
  • 1
    You are right, the native Picker does not allow you to apply your own styling. It looks for a label and an image in the items you supply and shows these in its own native ways. In the collapsed form, the image comes first, but in the menu list, the label comes first. I assume the large red circle in your screenshot is the Japanese flag? Try using smaller images to build the Picker items. To change the font size, these posts might help: for the collapsed form, for the menu items Commented Apr 6, 2024 at 8:28

1 Answer 1

0

Picking a smaller image worked fine for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.