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)
}
}
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
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
Why Is Swift Compile Time So Slow
Changing the Status Bar Color For Specific Viewcontrollers Using Swift in Ios8
Binary Operator * Cannot Be Applied to Operands of Type Int and Double
Arkit - What Do the Different Columns in Transform Matrix Represent
Deleting a Row from a Uitableview in Swift
Providing a Default Value For an Optional in Swift
Get Associated Value from Enumeration Without Switch/Case
JavaScript Synchronous Native Communication to Wkwebview
Trapping Signals in a Swift Command Line Application
Xcode 8 Beta 3 Use Legacy Swift Issue
Swift Beta Performance: Sorting Arrays
Swift Equality Operator on Nested Arrays
Decrement Index in a Loop After Swift C-Style Loops Deprecated
How to Detect a Tap Gesture Location in Swiftui
Iterate Through Files in a Folder and Its Subfolders Using Swift'S Filemanager