Search code examples
swiftuiuisegmentedcontrolpicker

Change Segmented Controller properties in SwiftUI


I'm trying to create a Segmented Controller in SwiftUI. This is my code so far:

Picker(selection: $currentStatus, label: Text("Treatment Status")) {
            ForEach(status, id: \.self) {
                Text($0)
            }
        }
        .pickerStyle(SegmentedPickerStyle())

And this is what it looks like:

enter image description here

This is fine, but I'd like to change the Segmented Controller properties to make it look like this:

enter image description here

I tried looking up any tutorials on how to modify Segmented Controllers in SwiftUI but couldn't find anything. Any help would be highly appreciated!


Solution

  • Here is a view that looks like the above and acts like a picker, see if this will work:

    struct MinimalPickerView: View {
            @State var selected = "Pendiente"
            let labels = ["Pendiente", "Atendido", "Cancelado", "Ausente"]
        
            var body: some View {
                HStack {
                    ForEach(labels, id: \.self) { label in
                        VStack {
                            Text(label).padding(2)
                            Rectangle()
                                .frame(height: 7)
                                .foregroundColor((selected == label) ? .black : .clear)
                        }
                        .onTapGesture {
                            selected = label
                        }
                        .padding(2)
                    }
                }
            }
    
    }