15

Is there a way to disable the native keyboard avoidance on iOS14?

There is no keyboard avoidance in iOS13, so I want to implement my own, but when I do the native one on iOS14 is still active, so both my implementation and the native one run at the same time, which I don't want.

My deployment target is iOS13, so I need a solution for both iOS13 and iOS14.

pawello2222
  • 46,897
  • 22
  • 145
  • 209
Tropicano
  • 281
  • 2
  • 15
  • Does this answer your question? [iOS 14 SwiftUI Keyboard lifts view automatically](https://stackoverflow.com/questions/63958912/ios-14-swiftui-keyboard-lifts-view-automatically) – pawello2222 Jan 13 '21 at 11:57
  • No, I need it to work on iOS 13 also – Tropicano Jan 13 '21 at 15:50
  • There is no *keyboard avoidance* on iOS 13. And the above solution won't do anything on iOS 13, so it should work with your code. – pawello2222 Jan 13 '21 at 15:53
  • As I sad, I have written my own keyboard avoidance logic (which works on iOS 13), but I need to turn of the iOS 14 native keyboard avoidance – Tropicano Jan 13 '21 at 16:02

2 Answers2

22

You can use if #available(iOS 14.0, *) if you want to adapt the iOS 14 code so it compiles on iOS 13.

Here is an adapted version of this answer to work on both iOS 13 and iOS 14:

struct ContentView: View {
    @State var text: String = ""

    var body: some View {
        if #available(iOS 14.0, *) {
            VStack {
                content
            }
            .ignoresSafeArea(.keyboard, edges: .bottom)
        } else {
            VStack {
                content
            }
        }
    }

    @ViewBuilder
    var content: some View {
        Spacer()
        TextField("asd", text: self.$text)
            .textFieldStyle(RoundedBorderTextFieldStyle())
        Spacer()
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
  • For me, using iOS 13.2, I had to use `.all` instead of `.bottom`. Either way, thanks for posting an answer to this. – Sylar Jan 31 '22 at 07:11
  • 3
    Maybe for simple views like this (filled with just spacers) this works. But there may be a case when you have a complex view, with custom header and many texts/buttons/textfields. In that case the view might still get pushed up, despite using `.ignoreSafeArea` modifier, so the solution to that would be to embed your view in `GeometryReader` and add the modifier to it instead of the former – Levan Apakidze Feb 22 '23 at 10:50
0

If you're still having issues with content being pushed around by the keyboard, Levan's comment solved this for me.

Re-posting as an answer so it's easier to find.

var body: some View {
    GeometryReader { _ in
        VStack {
            //content
        }
        .sheet(isPresented: $showModal) {
            ModalWithTextFieldThatPushesUpParentContent()
        }
    }
    .ignoresSafeArea(.keyboard) //keyboard avoidance
}

This is especially annoying when a Sheet view has a textfield and the keyboard pushes the parent content up.

  1. Need to add the ignore code to the parent view
  2. Add a GeometryReader, even if you're not using it
William T.
  • 12,831
  • 4
  • 56
  • 53