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

Keep the iOS keyboard visible while switching between TextFields

Forums > SwiftUI

There are lots of threads about hiding the keyboard when you want to, but I can't find anything about keeping the keyboard in place.

The code below presents a simple form that I want the user to complete. The focus tracking works fine, every time the user hits return the next field comes in to focus. But, the keyboard tries to disappear every time which makes the entire UI bounce (In my production code the form is actually on a sheet). How can I prevent the keyboard from dropping onSubmit?

enum ListFocusable: Hashable {
    case field1
    case field2
    case field3
    case none

    func next() -> ListFocusable{
        switch self {
        case .field1:
            return .field2
        case .field2:
            return .field3
        case .field3:
            return .none
        case .none:
            return .none
        }

    }
}

struct ContentView: View {
    @State var field1 = ""
    @State var field2 = ""
    @State var field3 = ""
    @FocusState private var listFieldInFocus: ListFocusable?
    var body: some View {
        VStack{
            TextField("Field1", text: $field1)
                .focused($listFieldInFocus, equals: .field1)
            TextField("Field2", text: $field2)
                .focused($listFieldInFocus, equals: .field2)
            TextField("Field3", text: $field3)
                .focused($listFieldInFocus, equals: .field3)
        }
        .onSubmit {
            listFieldInFocus = listFieldInFocus!.next()
        }
    }
}

   

Maintaining the iOS keyboard visible while transitioning between text fields can enhance user experience, ensuring seamless interaction without the need for repetitive keyboard toggling. By implementing proper handling within the app's interface, such as utilizing appropriate text field delegates and managing the keyboard's appearance, developers can achieve this fluidity. Employing techniques like resigning first responder status programmatically and utilizing the UITextFieldDelegate methods can ensure that the keyboard persists while seamlessly switching between text fields, optimizing the user's workflow and reducing unnecessary distractions.

   

Thank you for the response. I dont know what it means, but UITextFieldDelegate and assigning first responder status look like new search terms.

   

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!

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.