HOWTO: Customize UIKeyboard

来源:互联网 发布:json怎么解析 编辑:程序博客网 时间:2024/06/05 01:19

转自:http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html


So for my application I needed to add a button to the number pad keyboard because I wanted users to be able to enter decimal values but the standard button keyboard doesnt include the decimal. And using a full keyboard has a lot of wasted keys that I had to restrict the user from hitting.

Then I came across this post that discussed the issue somewhat. 

http://www.iphonedevsdk.com/forum/ip...-keyboard.html

I wanted to elaborate on that post to show how I was able to actually access the view of the keyboard to add my own buttons.

Step 1: Follow the instructions in the above post to handle the messages sent when the the keyboard is going to be show/hidden. This will help you understand a little more about how the keyboard works.

For my use I didnt really need to know the information regarding position of the keyboard and all that, but it was handy to know when it is being show.

Step 2: Check out the code below that actually gets you a reference to the UIKeyboard view (UIView) which will allow you to add subviews.

Code:
//The UIWindow that contains the keyboard view - It some situations it will be better to actually//iterate through each window to figure out where the keyboard is, but In my applications case//I know that the second window has the keyboard so I just reference it directlyUIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];//Because we cant get access to the UIKeyboard throught the SDK we will just use UIView. //UIKeyboard is a subclass of UIView anywaysUIView* keyboard;//Iterate though each view inside of the selected Windowfor(int i = 0; i < [tempWindow.subviews count]; i++){//Get a reference of the current view keyboard = [tempWindow.subviews objectAtIndex:i];//Check to see if the className of the view we have referenced is "UIKeyboard" if so then we found//the keyboard view that we were looking forif([[keyboard className] isEqualToString:@"UIKeyboard"] == YES){//Keyboard is now a UIView reference to the UIKeyboard we want. From here we can add a subview//to th keyboard like a new button//Do what ever you want to do to your keyboard here...}}
I put the above code inside of the appDelegate into a method that I call the first time that the user shows the textbox (figured that out using the didShow method in step1). 

Im not much of a tutorial writter, but hopefully that will shine some light on how to add custom items to your keyboards 




UPDATE:

Apparently className does not work with the 2.1 firmware or in all conditions so as per the article linked above I would recomend a change in the code i provided.

Code:
if([[keyboard className] isEqualToString:@"UIKeyboard"] == YES)
that if statement should change. Instead of using [keyboard className] I used the following line of code

Code:
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)

原创粉丝点击