2

I am currently building my first iOS app with Swift UI and I was wondering if there is any standard way in Swift UI to allow for single selection in a list. Just like is the case in the iOS settings (see screenshot below).

1

Thanks for your help in advance!

3

2 Answers 2

0

Meanwhile found an answer thanks to example code on hackingwithswift.com

struct ContentView: View {
var strengths = ["Mild", "Medium", "Mature"]

@State private var selectedStrength = 0

var body: some View {
    NavigationView {
        Form {
            Section {
                Picker(selection: $selectedStrength, label: Text("Strength")) {
                    ForEach(0 ..< strengths.count) {
                        Text(self.strengths[$0])

                        }
                    }
                }
            }.navigationBarTitle("Select your cheese")

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

1 Comment

Another way to tackle the issue is by building a list containing HStacks as described here: stackoverflow.com/a/59834220/14934149
0

This is what I used for single selection for SwiftUI ForEach

 @State private var selectedTimeSlotBtn: Int = 0
 @State private var timeSlots = ["15s","30s","60s","∞"]

 ForEach(0..<timeSlots.count) { item in
                                    Button {
                                        self.selectedTimeSlotBtn =item
                                    } label: {
                                        Text(timeSlots[item])
                                            .frame(width: 39, height: 26, alignment: .center)
                                            .foregroundColor(Color.white)
                                            .background(self.selectedTimeSlotBtn == item ? Color("clifie_light_green_com") : Color("clifie_dark_gray"))
                                            .cornerRadius(12)
                                            .padding(.trailing, 2)
                                            .padding(.leading, 2)
                                    }
                   }

Comments

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.