简体   繁体   中英

Showing iOS onscreen keyboard without a TextField

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, eg one for receiving input from the keyboard. Which one would that be?

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM