NEW: Learn to build the incredible iOS 15 Weather app today! >>

TextEditor Keyboard Avoidance

Forums > SwiftUI

Xcode 13.1 SwiftUI Macbook Pro macOS Monterey 12.0.1 iOS 15

PROBLEM

I'm aware that SwiftUI now handles keyboard avoidance. In my following example. The TextEditor at the bottom is covered by the keyboard. It shifts the view up but not enough.

import SwiftUI

struct ContentView: View {
  @State private var name: String = "Placeholder"

  var body: some View {
    VStack {
      Color.red
        .frame(height: 314)

      Color.yellow
        .frame(height: 314)

    TextEditor(text: $name)
    }
    .padding()
  }
}

I noticed that when I change this from a VStack to a Form. It completely breaks the keyboard avoidance such as in:

import SwiftUI

struct ContentView: View {
  @State private var name: String = "Placeholder"

  var body: some View {
    Form {
      Color.red
        .frame(height: 314)

      Color.yellow
        .frame(height: 314)

    TextEditor(text: $name)
    }
    .padding()
  }
}

Can someone shed some light on this for me? My app is having a similar problem and I think I have isolated the behavior to this.

   

Put the VStack in a ScrollView

you might want height frame on TextEditor

   

I got this to work following https://stackoverflow.com/questions/56491881/move-textfield-up-when-the-keyboard-has-appeared-in-swiftui

But when I try to implement this I get this error.

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a table view cell's content view. We're considering the collapse unintentional and using standard height instead. Cell: <SwiftUI.ListTableViewCell: 0x7fe847091c00; baseClass = UITableViewCell; frame = (0 211.333; 388 44); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x6000025a35c0>> 2021-11-30 07:26:50.360170-0800 KeyboardAvoidance[59379:902568] Writing analzed variants.

   

How would I go about that in this example?

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.