2

I have a TextField in my iOS app, and it is positioned such that I don't want it to move when the keyboard appears. However, the view is autoresizing when the keyboard appears. Is there a way to prevent this?

import SwiftUI

struct test: View {
    @State var text: String = ""
    var body: some View {
        TextField("Type", text: self.$text)
    }
}
Maya Reese
  • 81
  • 1
  • 5

1 Answers1

9

You can use a modifier to tell a certain view to ignore specific or all iOS safe areas. Apply the following .ignoresSafeArea(.keyboard) to the parent view, and it will not resize when the keyboard is open.

  • 1
    Thanks for this! I had a modal on a modal (I presented a share sheet and the share sheet presented Messages) and my underlying view was getting squished. Putting this on there fixed it! – Nate Lowry Sep 27 '22 at 18:16