UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

decimalPad and custom button for minus sign

Forums > SwiftUI

I'm trying to add a custom button to the decimalPad so that I can input negative integers for various fields, but I'm stuck. I have no idea what to put in the Button code. I've seen other apps do this and I tried searching on the Internet but couldn't really find anything relavent. I know a workaround is to use the numbersAndPunctuation keyboard but I'd prefer the decimalPad. Can someone please help?

struct ContentView: View {
    @State private var amount1 = 0
    @State private var amount2 = 0
    @State private var amount3 = 0
    @FocusState var amountIsFocused: Bool

    var body: some View {
        NavigationView{
            Form {
                TextField("Amount 1", value: $amount1, format: .number)
                    .focused($amountIsFocused)
                    .keyboardType(.decimalPad)
                TextField("Amount 2", value: $amount2, format: .number)
                    .focused($amountIsFocused)
                    .keyboardType(.decimalPad)
                TextField("Amount 3", value: $amount3, format: .number)
                    .focused($amountIsFocused)
                    .keyboardType(.decimalPad)
            }
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    Button("-") {

                    }
                    Button("Done") {
                        amountIsFocused = false
                    }
                }
            }
        }
    }
}

2      

I played around with this some more and still haven't figured out how to implement it on the keyboard. This is a step in the direction, I'm trying to go, though it seems like duct tape coding. There's got to be a better way to do this.

enum WhichAmount {
    case one, two, three
}

class Amounts: ObservableObject {
    @Published var amount1 = 0
    @Published var amount2 = 0
    @Published var amount3 = 0
}

struct MinusView: View {
    @ObservedObject var amounts: Amounts
    let whichAmount: WhichAmount
    @FocusState var amountIsFocused: Bool

    var body: some View {
        HStack {
            TextField("Enter value:", value: whichAmount == .one ? $amounts.amount1 : whichAmount == .two ? $amounts.amount2 : $amounts.amount3, format: .number)
                .keyboardType(.decimalPad)
                .focused($amountIsFocused)
            Button("-") {
                switch whichAmount {
                case .one:
                    amountIsFocused = false
                    amounts.amount1 *= -1
                case .two:
                    amountIsFocused = false
                    amounts.amount2 *= -1
                case .three:
                    amountIsFocused = false
                    amounts.amount3 *= -1
                }
            }
        }
    }
}

struct ContentView: View {
    @StateObject var amounts = Amounts()
    @FocusState var amountIsFocused: Bool

    var body: some View {
        NavigationView{
            Form {
                MinusView(amounts: amounts, whichAmount: .one)
                MinusView(amounts: amounts, whichAmount: .two)
                MinusView(amounts: amounts, whichAmount: .three)
            }
        }
    }
}

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.