0

This is how i use it in code:

@State var selectedType: BiblePlanOffsetType = .verse
@State var types = [BiblePlanOffsetType.verse, .chapter]

Picker("", selection: $selectedType) {
    ForEach(types, id: \.self) { elem in
        Text(elem.title)
    }
}
.pickerStyle(.segmented)

enum BiblePlanOffsetType: Int16, CaseIterable {
    case verse = 0
    case chapter = 1
        
    var title: String {
        switch self {
        case .verse:
            return "\(Image(systemName: "quote.opening")) " + "verses".localized
        case .chapter:
            return "\(Image(systemName: "text.quote")) " + "chapters".localized
        }
    }
}

Why it results with no icon displayed?

enter image description here

According to this article, it should work.

3

1 Answer 1

0

title should be a LocalizedStringKey. You can directly use Images as part of its string interpolation:

var title: LocalizedStringKey {
    switch self {
    case .verse:
        return "\(Image(systemName: "quote.opening")) verses"
    case .chapter:
        return "\(Image(systemName: "text.quote")) chapters"
    }
}

Now you don't even need your own .localized. Note that the localisation keys are %@ verses and %@ chapters, so change your xcstrings accordingly.


If you want to use your own .localized for some reason (you really shouldn't), title could return a Text,

var title: Text {
    switch self {
    case .verse:
        return Text(Image(systemName: "quote.opening")) + Text("verses".localized)
    case .chapter:
        return Text(Image(systemName: "text.quote")) + Text("chapters".localized)
    }
}

That said, a segmented Picker does not support displaying Image + Text. It's got to be image only or text only. You can use an ImageRenderer to combine the image and text into one Image as a workaround, or design your own picker. See more details in this question.

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

2 Comments

I don't think the SF symbol will actually render in the segmented Picker. Have you tested this? From what I know, you can show a symbol only using .palette style, but not both text and symbol.
@AndreiG. You're right. I disregarded the context. If the question is ultimately asking "how to use image + text in a segmented picker" though, this would be a duplicate of this.

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.