Home > Back-end >  iOS16 Bug Keyboard breaks layout on sheet dismissal SwiftUI
iOS16 Bug Keyboard breaks layout on sheet dismissal SwiftUI

Time:10-12

In iOS16 faced a bug with keyboard inside sheet, when sheet is dismissing keyboard disappears(what is ok), but layout is not updated. I saw only 1 question on same problem and wondering maybe somebody found a temporary workaround until Apple don't fix this. Code to reproduce :

struct Test: View {
    
    @State var isPresented: Bool = false
    @State var text: String = ""
    
    var body: some View {
        VStack{
            Button {
                isPresented.toggle()
            } label: {
                Text("PRESENT")
            }
        }
        .sheet(isPresented: $isPresented) {
            ZStack {
                Color.red
                VStack{
                    TextField("Test", text: $text)
                        .frame(height: 50, alignment: .center)
                    Spacer()
                    Rectangle()
                        .fill(Color.blue)
                        .frame(width:300, height: 50)
                }
            }
        }
    }
}

Video: https://vimeo.com/758845068

CodePudding user response:

The .ignoresSafeArea() fixes the issue, but... This will have as result of keyboard overlap in your UI and not be able to scroll to see all your elements.

I use the .adaptsToKeyboard() custom modifier taken from this answer

and then using it where needed with this particular order.

VStack {...}
       .adaptsToKeyboard()
       .ignoresSafeArea()
  • Related