NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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
                    }
                }
            }
        }
    }
}

1      

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)
            }
        }
    }
}

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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

Reply to this topic…

You need to create an account or log in to reply.

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.