Krakke93
Krakke93

Reputation: 95

Select a value for a Picker by tapping on the whole row

I'm building a SwiftUI app that has a Picker view. It is embedded inside a NavigationView, so as I tap on the title of the Picker, I get forwarded to a fullscreen selection view to select a value. I can select one only by tapping on the Text itself, but nothing happens if I tap outside the red area. areas of interaction on a screen

I wonder is this an expected behaviour or some kind of a bug? Can it be fixed so tap on any part of the row will count as a selection? The code is following:

var body: some View {
    NavigationView {
        Form {
            Section {
                TextField("Amount", text: $checkAmount)
                    .keyboardType(.decimalPad)
                // !!!
                // The picker is here
                // !!!
                Picker("Number of people", selection: $peopleIndex) {
                    ForEach(peopleRange) {
                        Text("\($0) people")
                    }
                }
            }

            Section(header: Text("How much tip do you want to leave?")) {
                Picker("Tip percentage", selection: $percentageIndex) {
                    ForEach(0 ..< tipPercentages.count) {
                        Text("\(self.tipPercentages[$0])%")
                    }
                }.pickerStyle(SegmentedPickerStyle())
            }

            Section {
                Text("$\(totalPerPerson, specifier: "%.2f")")
            }
        }.navigationBarTitle("WeSplit")
    }
}

Upvotes: 2

Views: 728

Answers (4)

Igor
Igor

Reputation: 109

Try .contentShape(Rectangle())

ForEach(peopleRange) {
    HStack {
        Text("\($0) people")
        Spacer()
    }
    .contentShape(Rectangle())
}

Upvotes: 1

E.Coms
E.Coms

Reputation: 11531

Hope this help you or give you some ideas.

  Picker("Number of people", selection: $peopleIndex) {
                ForEach(1..<100) { index in
                    ZStack{
                        Color.white
                        Text("\(index) people")
                    }

                }
            }

Upvotes: 0

Francesco Deliro
Francesco Deliro

Reputation: 3939

If you want to select all the row you can use an HStack with Spacer:

ForEach(peopleRange) {
    HStack {
        Text("\($0) people")
        Spacer()
    }
}

Upvotes: 0

Krakke93
Krakke93

Reputation: 95

It seems that this behaviour is a bug and will be fixed in the Xcode 11.2

Upvotes: 0

Related Questions