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 - How to prevent keyboard in a sheet to push up my main UI
I had a similar problem once. I can't reproduce your code, but you might try using GeometryRadar like this:
GeometryReader { geometry in
ZStack {
VStack {
// This is the part that moves up and down
}
}
}
.ignoresSafeArea(.keyboard, edges: .bottom)
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)
}
}
A thin line appears when the keyboard dismisses - SwiftUI
Try using .ignoresSafeArea(.keyboard)
on your view embedding the textfield (not the textfield itself), and let the view take the whole space by adding Spacer()
to the bottom of your view.
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)
}
SwiftUI Keyboard Dismissing Issues
Just apply the tap gesture to the background. Now you can dismiss anything by tapping the blue background:
struct ContentView: View {
@State private var favoriteColor = 0
@State private var text1: String = ""
@State private var text2: String = ""
var body: some View {
VStack {
TextField("Type Things", text: $text1)
TextField("Type More Things", text: $text2)
Picker("What is your favorite color?", selection: $favoriteColor) {
Text("Red").tag(0)
Text("Green").tag(1)
}
.pickerStyle(.segmented)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(
Color.blue
.opacity(0.4)
.onTapGesture {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
)
}
}
Result:
Related Topics
Xcode 8 with Mixed Swift and Objective-C Project Generated "Modulename-Swift.H" Header Not Found
Has Anyone Found a Good Way of Using the New iOS5 Keyboard Events
Is It Safe to Delete SQLite's Wal File
How to List (Almost) All Emojis in Swift for iOS 8 Without Using Any Form of Lookup Tables
iPad - Parsing an Extremely Huge JSON - File (Between 50 and 100 Mb)
Perform UI Changes on Main Thread Using Dispatch_Async or Performselectoronmainthread
iOS 10 Rich Media Push Notification (Media Attachment) in Objective-C
Restkit and Saving to Coredata as Nsmanagedobject
Drawing Gradient Over Image in iOS
Getting a Thumbnail of a *.Mov Video iOS
Move Gmsmarker on Google Map Like Uber
Change the Listing Order of the View Controllers in Xcode Storyboard
How to Pass Swift Enum with @Objc Tag
How to Restrict the iOS App Only for iPhone Excluding iPad
Custom Init of Uiviewcontroller from Storyboard
Tab Bar Background Is Missing on iOS 7.1 After Presenting and Dismissing a View Controller