转自: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.
//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 directly
UIWindow* 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 anyways
UIView* keyboard;
//Iterate though each view inside of the selected Window
for(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 for
if([[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...
}
}
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.
if([[keyboard className] isEqualToString:@"UIKeyboard"] == YES)
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
本文详细介绍了如何通过编程自定义iPhone的键盘,以便用户能够输入小数值,而无需使用标准键盘。文章提供了关键步骤和代码示例,帮助开发者了解如何将自定义按钮添加到键盘上,从而提高用户体验。
917

被折叠的 条评论
为什么被折叠?



