Why does my TextEditor in SwiftUI ignore my .keyboardType?

Viewed 497

I am trying to use a TextEditor() in SwiftUI and will need .keyboardType(.numberPad). Unfortunately though, whenever I set this option, for some reason my device and simulator seem to ignore it entirely! It just shows the default keyboard no matter what I set as the keyboard option.

Does anyone know why this might be? I have tested iOS 14.0 and 14.2 Beta with Xcode 12 Beta 2.

2 Answers

Here's an ugly workaround using SwiftUI-Introspect

(FB8816771 is the feedback ID I logged with Apple.)

private extension View {
    func keyboardType_FB8816771(_ type: UIKeyboardType) -> some View {
        let customise: (UITextView) -> () = { uiTextView in
            uiTextView.keyboardType = type
        }
        
        return introspect(selector: TargetViewSelector.siblingContaining, customize: customise)
    }
}

In case this helps anyone with the fact that TextEditor does not have a Done button...expanding on the answer from Luke Howard above...

import Introspect

extension View {
    func addDoneButton() -> some View {
        let helper = MainViewHelper()
        
        let customise: (UITextView) -> () = { uiTextView in
            let toolBar = UIToolbar(frame: CGRect(x: 0.0,
                                                  y: 0.0,
                                                  width: UIScreen.main.bounds.size.width,
                                                  height: 44.0))//1
            let flexible = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
            let barButton = UIBarButtonItem(title: "Done", style: .plain, target: uiTextView, action: #selector(helper.close))
            toolBar.setItems([flexible, barButton], animated: false)//4
            uiTextView.inputAccessoryView = toolBar
            uiTextView.keyboardAppearance = .light
        }
        
        return introspect(selector: TargetViewSelector.siblingContaining, customize: customise)
    }
}

class MainViewHelper {
    @objc func close() {
        
    }
}
Related