HOWTO: Customize UIKeyboard

本文详细介绍了如何在iOS应用中自定义键盘,包括获取键盘视图并添加自定义按钮的方法,适合开发者深入理解iOS键盘的工作原理及自定义实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: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 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...
		}
	}
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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值