SwiftUI TextEditor dismiss of onscreen keyboard does not show the text that is after the cursor

Scenario: Use code below or similar. Use Xcode simulator or physical device. Have on-screen keyboard hidden. Tap at the end of one of the lowest 5 lines of text that are visible. Using the iPad 10th gen simulator in landscape orientation, this is likely line 14 or so. The on-screen keyboard should have displayed. The text in the TextEditor may scroll up a bit. Using the bottom right key of the on-screen keyboard, dismiss the keyboard. The lines of text after the cursor position are not shown making the TextEditor about half-blank (when in landscape orientation).

How do I avoid this unexpected behavior?

I'm currently using Xcode 15.1. The code below is just test code to demonstrate the issue.

import SwiftUI

let alpha = "abcdefghijklmnopqrstuvwxyz"
let araAlpha = Array(alpha).map { String($0) }

struct ContentView: View {
  @State var text = ""
  var body: some View {
    TextEditor(text: $text)
      .padding(60).font(.largeTitle)
      .onAppear {
        for i in 1 ... 40 {
          let n = Int.random(in: 5 ... 20)
          text += String(i) + " "
            + araAlpha.shuffled()
            .joined().prefix(n) + "\n"
        }
      }
  }
}
  • It'd be super nice if someone would test this to see if this only happens for me. Should only take 5 minutes.

Add a Comment