Reputation: 12043
I have a special iPad app where the onscreen keyboard must remain visible even if there is no UITextField being the first responder.
Supposedly, the keyboard appearing is controlled by UITextInputTraits.
So I've made my ViewController implement the UITextInputTraits protocol and added these methods to it:
- (UITextAutocapitalizationType) autocapitalizationType { return UITextAutocapitalizationTypeNone; }
- (UITextAutocorrectionType) autocorrectionType { return UITextAutocorrectionTypeNo; }
- (UITextSpellCheckingType) spellCheckingType { return UITextSpellCheckingTypeNo; }
- (UIKeyboardAppearance) keyboardAppearance { return UIKeyboardAppearanceDefault; }
- (UIKeyboardType) keyboardType { return UIKeyboardTypeAlphabet; }
- (UIReturnKeyType) returnKeyType { return UIReturnKeyDefault; }
- (BOOL) enablesReturnKeyAutomatically { return NO; }
- (BOOL) secureTextEntry { return NO; }
Lastly, in the controller's viewDidAppear
method I'm invoking [self becomeFirstResponder]
But that's apparently not enough to bring up the keyboard. What am I missing?
I do even get a call to my keyboardAppearance
method, but the keyboard does not come up (or disappears when it was up) regardless.
I wonder if I have to implement another protocol, e.g. one for receiving input from the keyboard. Which one would that be?
Upvotes: 2
Views: 2233
Reputation: 331
A quicker solution to show the keyboard from your view controller, using becomeFirstResponder
method, is to implement UIKeyInput
protocol and return YES
for -(BOOL)canBecomeFirstResponder
in your view controller. UIKeyInput
protocol methods will be used in this case for handling keyboard actions.
Also be careful to return YES
for canBecomeFirstResponder
only when needed to avoid weird behaviour.
Upvotes: 2
Reputation: 12043
Turns out that it's not enough to implement UITextInputTraits but one need to implement the UITextInput protocol so that one can react to typing on the keyboard.
After changing the implemented protocol in my view controller's interface declaration to UITextInput
, and adding these properties, the keyboard appears - only for typing more methods neede to be implemented:
@property (nonatomic, readonly) UITextRange *markedTextRange;
@property (readwrite, copy) UITextRange *selectedTextRange;
Upvotes: 1