How to pull up a UIKeyboard without a UITextField or UITextView?
Asked Answered
I

5

12

I'm currently developing an OpenGL ES game for the iPhone and iPod touch.

I was wondering how I can easily pull up the UIKeyboard? Is there an official, documented possibility to pull up a UIKeyboard without using a UITextField of UITextView?

Inequity answered 3/9, 2009 at 21:41 Comment(1)
possible duplicate of How to display the iPhone/iPad keyboard over a full screen OpenGL ES appAnisette
F
14

It's not "officially" possible - you can't access the UIKeyboard object at all while staying within Apple's guidelines.

Creating an invisible UITextField, then calling [textField becomeFirstResponder] would do the job - you could even subclass UITextField first, then override the delegate method textField:shouldChangeCharactersInRange: to redirect the text input to where you want it to go.

Footstalk answered 7/9, 2009 at 14:57 Comment(2)
See more up-to-date answer from @Weaverbird using 3.2+ APICrystacrystal
This answer is now wrong. See here for an example of how to doExpectoration
W
19

If you subclass UIResponder, and declare the UIKeyInput protocol, the keyboard will appear when you become the firstResponder.

See the UIKeyInput protocol here.

One thing that tripped me up is that you'll need to override the canBecomeFirstResponder message.

Weaverbird answered 26/9, 2011 at 2:23 Comment(2)
Only UIView subclasses, not just any UIResponder subclass. I tried to make my AppDelegate (a UIResponder) subclass, show a keyboard and failed.Arlon
After subclass of UIView, what's the different between an UITextfield ? they are all have to be added to the screen, then become the first responder, show the keyboard.Taylor
F
14

It's not "officially" possible - you can't access the UIKeyboard object at all while staying within Apple's guidelines.

Creating an invisible UITextField, then calling [textField becomeFirstResponder] would do the job - you could even subclass UITextField first, then override the delegate method textField:shouldChangeCharactersInRange: to redirect the text input to where you want it to go.

Footstalk answered 7/9, 2009 at 14:57 Comment(2)
See more up-to-date answer from @Weaverbird using 3.2+ APICrystacrystal
This answer is now wrong. See here for an example of how to doExpectoration
R
3

It is indeed possible. I had myself struggled a lot with this. UIKeyInput protocol can be used to pull a keyboard without using a UITextField or UITextView. However it is only available in iOS 3.2 and above. Here is a tutorial on that.

Hope that helps.

Ravo answered 19/2, 2012 at 5:1 Comment(0)
D
1

I displayed the keyboard without any visible UITextField by positioning my textfield's frame out of the visible screen:

#define TEXT_FRAME                 -50,-50,0,0

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(TEXT_FRAME)];

When I set the "first responder" the keyboard becomes visible without any visible input area:

// show the keyboard    
[self.textField becomeFirstResponder];

I later dropped the idea. However, I don't know its conformance to the Apple guidelines.

Dewitt answered 22/6, 2011 at 8:45 Comment(0)
E
0

I haven't tried it but http://www.cocoadev.com/index.pl?UIKeyboard shows some code that looks like it should work.

Exhaustive answered 3/9, 2009 at 22:22 Comment(3)
I don't think UIKeyboard is officially exposed in the SDK, so perhaps to be "legal" you have to use a hidden UITextField.Platen
@David - So it's not possible then?Inequity
The code you posted does not work. While it may be valid, the compiler will let you know that you can't access the UIKeyboard class.Cartridge

© 2022 - 2024 — McMap. All rights reserved.