3

I am trying to create a form picker that shows the currently selected image resource at the top level and when the user selects it to show the detail, I want it to show all of the image resources available.

Here is the relevant section of code:

                Picker("Background image:", selection: $task.background) {
                    ForEach(0 ..< backgroundImages.count) {
                        Image("Background\($0)").resizable().frame(width: 100, height: 35, alignment: .center)
                        Text("Background\($0)")
                    }
                }

The problem with this is that in the detail screen I get:

enter image description here

The image is blank and the image and text appear on 2 different rows.

I have tried wrapping the Image and Text lines in an HStack, but that gives a compile time error on some other line. Any suggestions would be helpful.

1 Answer 1

1

The following should compile & work well (tested with replaced system images, Xcode 11.2)

demo

Picker("Background image:", selection: $task.background) {
    ForEach(0 ..< backgroundImages.count) { i in
        HStack {
            Image("Background\(i)").resizable().frame(width: 100, height: 35, alignment: .center)
            Text("Background\(i)")
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for that response. That fixes the first half of my problem in that it displays the Text and image on the same row. Unfortunately the image, while displayed correctly in the Master view, is still a white rectangle in the detail view. Remember that this is a Picker in a form so that it does not display as a wheel picker, but as a Master/Detail picker.

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.