SwiftUI Dismiss keyboard in modal issue

Hello,

I have an issue with the automatic keyboard dismissing in a modal : If I present a modal with textfields, and I try to dismiss the modal to close the keyboard, the keyboard disappear (that's good) but it leave some space...

Here is the example :

And here is my code to reproduce the issue (iOS 16, real device) :

struct ContentView: View {
    @State private var isViewPresented: Bool = false

    var body: some View {
        Button {
            isViewPresented = true
        } label: {
            Text("Show View")
        }
        .sheet(isPresented: $isViewPresented) {
            FormView()
        }
    }
}

struct FormView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: 32) {
                    TextField("Text 1", text: .constant(""))
                        .frame(height: 42)
                        .background(Color.red)
                    TextField("Text 2", text: .constant(""))
                        .frame(height: 42)
                        .background(Color.red)
                    TextField("Text 3", text: .constant(""))
                        .frame(height: 42)
                        .background(Color.red)
                    TextField("Text 4", text: .constant(""))
                        .frame(height: 42)
                        .background(Color.red)
                }
            }
            .background(Color.yellow)
            .navigationTitle("Form view")
        }
    }
}

As you can see, when I start to dismiss the modal, the keyboard disappear and then there is a black space who appears...

Any suggestions ?

Thanks,

Alexandre

Accepted Reply

Hello everyone! It seems that this issue is fixed by Apple from iOS 16.4! At least, the code I posted previously is now working fine and the bug is not reproducible anymore.

Replies

I also see same issue on iOS 16 only but i see white background stays on my home screen and everything is shifted above.

Another thing is that my app open webkit and uses autofill input textfields and dismisses then i see white background overlay. I did not find any solution so far because its not controllable from app.

Any updates?

  • Unfortunately not; I hope Apple will fix this quickly...

Add a Comment

I stuck on the same issue in a sheet.

Found a solution, it works well for me. https://stackoverflow.com/questions/56491881/move-textfield-up-when-the-keyboard-has-appeared-in-swiftui/60178361#60178361

  • I tried the solution on my project, but still the same issue: when I dismiss the keyboard with the modal swipe down, a white space appear. I didn't found any generic solutions for now.

Add a Comment

I found solution

var body: some View {

        GeometryReader { _ in
            //your view
            }
           .ignoresSafeArea(.keyboard, edges: isKeyboardVisible ? .top : .bottom)
}

For detecting if keyboard is visible or not you can use any solution that you like. as example https://stackoverflow.com/questions/65784294/how-to-detect-if-keyboard-is-present-in-swiftui

Hello everyone! It seems that this issue is fixed by Apple from iOS 16.4! At least, the code I posted previously is now working fine and the bug is not reproducible anymore.

The worst part of this bug (below 16.4), it's not only cosmetic, but the area below the keyboard becomes inactive (can't interact with the screen when you see this ghost area of the keyboard).

  • I am experiencing exact same issue on iOS 17.0 and iOS 17.3 , have you found any good workarounds?

Add a Comment

Hey, I am still able to reproduce the issue on iOS 17, does anyone experiencing similar issue?