0

Upon navigating to a view, I want the number pad to already be raised. Right now I have a solution that works the first time (albeit with a delay) but fails to raise the number pad if the user navigates back a second time. Is there a better way to raise the number pad in SwiftUI (or to have it always up)?

Example Code:

struct ParentView: View {
    @FocusState var numberPadFocused: Bool
    @State var isActive: Bool = false
    var body: some View {
        NavigationView {
            VStack {
                Button {
                    numberPadFocused = true
                    isActive = true
                    print("Called")
                } label: {
                    Text("Navigate")
                }
            
                NavigationLink(destination: ChildView(focusState: $numberPadFocused), isActive: $isActive) { Color.white }
            }
        }
    }
}

struct ChildView: View {
    @State var text: String = ""
    @FocusState.Binding var focusState: Bool
    var body: some View {
        TextField("Enter Number...", text: $text)
            .keyboardType(.numberPad)
            .focused($focusState)
    
    }
}
John Sorensen
  • 710
  • 6
  • 29
  • What would happen if you added an `.onAppear { focusState = true }` into your ChildView? – ScottM Dec 31 '21 at 18:50
  • I tried that. If you can get that to work post a MRP – John Sorensen Dec 31 '21 at 19:08
  • For what you are trying to do, you should just make a custom keyboard and implement it as part of your view. There is nothing that special about the stock number pad, except that it reliable raises and lowers depending upon focus state. – Yrb Jan 01 '22 at 15:57
  • That's what I'm thinking now too. I wanted to use the default keyboard because, at least according to this post (see below), it will allow for autofilling of my Auth SMS verification code (I'm using phone auth and this number pad is for that view). https://stackoverflow.com/questions/50772090/how-to-show-verification-code-suggestion-on-keyboard-from-message – John Sorensen Jan 01 '22 at 21:42
  • But at this point that convenience might have to be sacrificed for a UI that works. – John Sorensen Jan 01 '22 at 21:43

0 Answers0