How to pull up a UIKeyboard without a UITextField or UITextView?

孤人 提交于 2019-11-27 20:14:54

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.

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!