NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

TextField is not moved back down with keyboard avoidance on

Forums > SwiftUI

Hi,

Does any know what I've done wrong here?

Problem

When I tap on the text field and the keyboard appears. Tap on the navgation link to get to the second screen. Then go back, the text field has not returned to the "non-focused" position.

  • iOS: 16
  • Sim: iPhone 14 Pro

Expectation

I am expecting to see the text field back at its original starting place. That is, when I tap the field, the keyboard avoidance causes that field to move up. Then I tap return on the keyboard to dismiss the keyboard, the text field returns to its starting position. When I navigation between view, I expect the same behaviour because the keyboard has been dismissed.

Steps

  • Tap the textField (assuming software keyboard is on)
  • Tap the navigationLink
  • Tap Back
import SwiftUI

struct ContentView: View {
    @State private var text: String = "Hello, world!"

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink("Hello", destination: { Text("World") })

                TextField("Whoops", text: $text)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.