Swiftui in iOS14 Keyboard Avoidance Issues and Ignoressafearea Modifier Issues

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

Demo

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

demo

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:

Result



Related Topics



Leave a reply



Submit