4
votes

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.

1
No, I need it to work on iOS 13 also - SAndreev
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
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 - SAndreev

1 Answers

6
votes

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()
    }
}