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

Answered by AlexandreCools in 749714022

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.

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?

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

Accepted Answer

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

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

SwiftUI Dismiss keyboard in modal issue
 
 
Q