SwiftUI submitLabel not working on TextEditor

Solution 1:

Upcoming Support

  • Support was added in Xcode 13.2 beta 3, which is not yet available.

Temporary Fix

Ok, so based on my attempts, .submitLabel(...) doesn't function with TextEditor. There is a possible solution where you add a Text(...) as a subview to the TextEditor(...) but that is extremely jenk and I wouldn't recommend that. There is however a solution I found for iOS 13 that seems to function the exact same way that the TextEditor appears to work and that's to use UIViewRepresentable. Thankfully it's very easy to implement. Create this struct.

struct TextView: UIViewRepresentable {
    
    typealias UIViewType = UITextView
    var configuration = { (view: UIViewType) in }
    
    func makeUIView(context: UIViewRepresentableContext<Self>) -> UIViewType {
        UIViewType()
    }
    
    func updateUIView(_ uiView: UIViewType, context: UIViewRepresentableContext<Self>) {
        configuration(uiView)
    }
}

Then use that view as you would use a TextView() where you assign its properties. This is effectively pulling over the UIKit version of a textView, so its properties will be the same. You can get fancy with it at this point and mix SwiftUI and UIKit properties and modifiers.

    TextView { view in
        view.text = sampleText
        view.returnKeyType = .done
    }