UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

3      

Put the VStack in a ScrollView

you might want height frame on TextEditor

3      

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.

3      

How would I go about that in this example?

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.