swiftuikeyboardios14

SwiftUI iOS14 - Disable keyboard avoidance


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.


Solution

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