TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: WeSplit: number of people doesn't scroll

Forums > 100 Days of SwiftUI

Here's my ContentView. If try to changed the number of people, that view doesn't scroll. Thoughts?

struct ContentView: View { let minNumberOfPeople = 2 let maxNumberOfPeople = 20

var dollarFormat : FloatingPointFormatStyle<Double>.Currency {
    let myCurrency = Locale.current.currencyCode ?? "USD"
    return FloatingPointFormatStyle<Double>.Currency( code: myCurrency )
}

@State private var checkAmount = 0.0
@State private var numberOfPeopleIndex = 2
@State private var tipPercentage = 20
@FocusState private var amountIsFocused: Bool

let tipPercentages = [0, 10, 15, 18, 20, 25]

var totalPeople: Int {
    minNumberOfPeople + numberOfPeopleIndex
}

var totalPerPerson: Double {
    checkAmount * (1.0 + Double(tipPercentage)/100.0) / Double(totalPeople)
}
var body: some View {
    NavigationView {
        Form {
            Section(header: Text("How much tip do you want to leave?")) {
                TextField("", value: $checkAmount, format: dollarFormat)
                    .keyboardType(.decimalPad)
                    .focused($amountIsFocused)
            }

            Section(header: Text("How much tip do you want to leave?")) {
                Picker("", selection: $tipPercentage) {
                    ForEach(tipPercentages, id: \.self) {
                        Text($0, format: .percent)
                    }
                }.pickerStyle(.segmented)
            }

            Section(header: Text("Number of People?")) {
                Picker("", selection: $numberOfPeopleIndex) {
                    ForEach(minNumberOfPeople ..< maxNumberOfPeople) {
                        Text("\($0) people")
                    }
                }
            }

            Section(header: Text("Each person contributes")) {
                Text(totalPerPerson, format: dollarFormat)
                    .foregroundColor(Color.red)
            }
        }
            .navigationTitle("WeSplit")
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    HStack {
                        Spacer()
                        Button("Done With Keyboard") {
                            amountIsFocused = false
                        }
                    }
                }
            }
    }
}

}

2      

I copied and pasted your code over my WeSplit App and it seemed to work fine. Which part isn't scrolling?

You do have the same section header twice:

Section(**header: Text("How much tip do you want to leave?"**)) {
                TextField("", value: $checkAmount, format: dollarFormat)
                    .keyboardType(.decimalPad)
                    .focused($amountIsFocused)
            }

Section(**header: Text("How much tip do you want to leave?"**)) {
                Picker("", selection: $tipPercentage) {
                    ForEach(tipPercentages, id: \.self) {
                        Text($0, format: .percent)
                    }
                }.pickerStyle(.segmented)
            }

2      

I have also tried your code and the Number of People section scroll fine.

Will say a couple of pointer about the code.

var dollarFormat is not just a dollar format it a local currency format so a better name might be something like

var localCurrency : FloatingPointFormatStyle<Double>.Currency {
    let myCurrency = Locale.current.currencyCode ?? "USD"
    return FloatingPointFormatStyle<Double>.Currency( code: myCurrency )
}

Section(header: Text("Each person contributes")) will be depreciated so should use

Section("Each person contributes") {
    Text(totalPerPerson, format: localCurrency)
        .foregroundColor(Color.red)
}

or for more complex headers and footers

Section {
    Text(totalPerPerson, format: localCurrency)
        .foregroundColor(Color.red)
} header: {
    Text("Each person contributes")
} footer: {
    Text("\(tipPercentage == 0 ? "Excluding" : "Including") tip")
}

2      

SOLVED! It was working all along. I was trying to scroll on my trackpad with a two finger scroll gesture. Instead, just use the "click with the thumb and scroll with a finger gesture."

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.