YosiFZ
YosiFZ

Reputation: 7900

SwiftUI Form picker with Image and Text

I have a Setting View in my app that provides an option to select a value from picker with this code:

var body: some View {
    NavigationView {
        Form {
            Section(header: Text("Widget Settings")) {
                Picker(selection: $chosenMediumType, label: Text("Medium Widget"), content: {
                    VStack {
                        Image(uiImage: UIImage(systemName: "sun.min")!).resizable().frame(width: 20, height: 20, alignment: .center)
                        Text("Sun")
                    }.tag(0)
                    VStack {
                        Image(uiImage: UIImage(systemName: "sunset")!).resizable().frame(width: 20, height: 20, alignment: .center)
                        Text("Sunset")
                    }.tag(1)
                    VStack {
                        Image(uiImage: UIImage(systemName: "moon")!).resizable().frame(width: 20, height: 20, alignment: .center)
                        Text("Moon")
                    }.tag(2)
                })
                .onChange(of: chosenMediumType) { print("Selected tag: \($0)") }
            }
        }
        .navigationBarTitle("Settings")
    }
}

When I click the picker row it's open the picker page and I can see each row with image and text, but In the settings, it makes the row bigger as the image shown: enter image description here

It is possible to use text only in the settings page and image+text in the picker view?

Upvotes: 4

Views: 1730

Answers (1)

OguzYuksel
OguzYuksel

Reputation: 132

view I just wanted to show you the way you can do it,

Just hide whole picker, it will stay have its without picker inside and overlay HStack, inside stack make a switch case or if or whatever you want

struct ContentView: View {

@State private var chosenMediumType = 0

var body: some View {
    NavigationView {
        Form {
            Section(header: Text("Widget Settings")) {
                Picker(selection: $chosenMediumType, label: Text("")
                       , content: {
                    VStack {
                        Image(uiImage: UIImage(systemName: "sun.min")!).resizable().frame(width: 20, height: 20, alignment: .center)
                        Text("Sun")
                    }.tag(0)
                    VStack {
                        Image(uiImage: UIImage(systemName: "sunset")!).resizable().frame(width: 20, height: 20, alignment: .center)
                        Text("Sunset")
                    }.tag(1)
                    VStack {
                        Image(uiImage: UIImage(systemName: "moon")!).resizable().frame(width: 20, height: 20, alignment: .center)
                        Text("Moon")
                    }.tag(2)
                       })
                    .hidden()
                    .overlay(
                        HStack(alignment: .center, spacing: nil, content: {
                            
                            Text("Medium Widget")
                            
                            Spacer()
                            
                            switch chosenMediumType {
                            case 1:
                            Text("Sunset")
                                .foregroundColor(.gray)
                            case 2:
                                Text("Moon")
                                    .foregroundColor(.gray)
                            default:
                                Text("Sun")
                                    .foregroundColor(.gray)
                            }
                        })
                    )
                    .frame(height: 30)
                

            }
        }
        .navigationBarTitle("Settings")
    }
}
    }

Upvotes: 1

Related Questions