Ios 14 Swiftui Keyboard Lifts View Automatically

iOS 14 SwiftUI Keyboard lifts view automatically

You should apply the modifier on the ZStack, NOT the NavigationView

NavigationView(content: {
ZStack{
,,,
}.navigationBarHidden(true)
.navigationBarTitle("")
.ignoresSafeArea(.keyboard, edges: .bottom) // <- This line moved up
})


Full working example:

struct ContentView: View {
@State var text = ""
var body: some View {
VStack{
Spacer()
Text("Hello, World")
TextField("Tap to test keyboard ignoring", text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
.padding()
.ignoresSafeArea(.keyboard, edges: .bottom)
}
}

Demo

SwiftUI iOS14 - Disable keyboard avoidance

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

SwiftUI | Stop TextField from moving up with keyboard automatically

Here is possible solution for your scenario. Tested with Xcode 12 / iOS 14

demo

var body: some View {
VStack {
Spacer()
TextField("Test", text: $textInput)
Spacer()
}
.ignoresSafeArea(.keyboard, edges: .bottom)
}

backup

How do you prevent SwiftUI from autoresizing view when keyboard appears

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.

SwiftUI squeezes parent view when modal shows keyboard

I found the solution to my problem here. The key was to set

.ignoresSafeArea(.keyboard, edges: .bottom)

on the parent view.



Related Topics



Leave a reply



Submit