UIKeyInput with All Caps Keyboard

682 views Asked by At

I have created a custom text input view and am trying to force the keyboard to show all caps on the keyboard; similar to a UITextField. Here is what I have tried, but it isn't working for me:

class CustomInput: UIView, UIKeyInput {

    // UIKeyInput inherits this property
    var autocapitalizationType: UITextAutocapitalizationType { 
        get { return .AllCharacters } 
        set { } 
    }

}

I hoped that overriding the variable and only allowing .AllCharacters would force the keyboard to all caps, but that isn't the case. Any ideas how I can get the keyboard to all caps?

2

There are 2 answers

2
jvmeer On

I have this same problem in an app I'm currently working on. Have spent a fair amount of time trying to figure this out, especially since autocorrectionType and keyboardType both seem to work fine.

My current work-around is to make my custom view implement UITextInput instead of UIKeyInput, with dummy code for all unused properties and functions. Extremely ugly work-around but it's the only way I can get it to work, so I'm running with it.

Would be interested to see if anyone has insight into the underlying issue here.

0
E000R On

I hadn’t seen this, but recently ran into the same problem and concluded that the UIKit functionality is broken and does not work as described. I submitted a feedback report with sample project demonstrating the problem, where if the custom view is replaced with a UITextView (with no other changes) everything works as expected:

FB13298309 (Keyboard: autocapitalizationType property is erroneously ignored for custom UIView conforming to <UIKeyInput>)

I've confirmed that providing a dummy implementation of the UITextInput protocol does allow this to work (iOS 17.0 here).

However, I find that using -reloadInputViews as described in the answer to Updating UITextView autocapitalizationType dynamically does not work when dynamically changing the property value. I found it necessary to have the view resignFirstResponder and immediately becomeFirstResponder after changing this property value of my custom class (e.g. in response to a button action), otherwise the onscreen keyboard does not change state until after the next keypress.