Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Form Picker with text and Image

Tags:

swift

swiftui

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.

like image 961
Ferdinand Rios Avatar asked Dec 06 '25 05:12

Ferdinand Rios


1 Answers

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)")
        }
    }
}
like image 166
Asperi Avatar answered Dec 07 '25 22:12

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!