为UIKeyboardTypeNumberPad增加自定义按键
2011-05-17 14:39
369 查看
If you have ever written an iPhone app that requires numeric input,then you surely know about the UIKeyboardTypeNumberPad. And if you haveever used that flavor of the iPhone's keyboard, then you sur
ely knowthat it lacks one very important feature: The UIKeyboardTypeNumberPaddoes not have a "return" key.
In fact every other keyboard type (except for the pretty similarUIKeyboardTypePhonePad) does offer the possibility to be dismissed bysetting the returnKeyType property of the correspondingUITextInputTraits implementor. So how does one achieve the same effectwith the number pad? We have found a workround!
When looking at the number pad, you'll notice that there is anunused space on its bottom left. That's where we are going to plug inour custom "return" key.
To make it short: take a screenshot, cut out the whole backspacekey, flip it horizotally, clear its backspace symbol in Photoshop andoverlay it with the text that we want on our “return” key. We’ve chosento label it “DONE”. Now we have the image for our custombutton’s UIControlStateNormal. Repeat the whole procedure (with atouched backspace key when taking the screenshot) to get a second imagefor our button’s UIControlStateHighlighted. Here’s the result:
Now back to coding. First we need to know when the number pad isgoing to be slided up on the screen so we can plug in our custom buttonbefore that happens. Luckily there’s a notification for exactly thatpurpose, and registering for it is as easy as:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[/pre]Don't forget to remove the observer from the notification center in the appropriate place once you're done with the whole thing:
[[NSNotificationCenter defaultCenter] removeObserver:self];[/pre]Now we’re getting to the heart of it. All we have to do in thekeyboardWillShow method is to locate the keyboard view and add ourbutton to it. The keyboard view is part of a second UIWindow of ourapplication as others have already figured out (see this thread).So we take a reference to that window (it will be the second window inmost cases, so objectAtIndex:1 in the code below is fine), traverse itsview hierarchy until we find the keyboard and add the button to itslower left:
- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}[/pre]Voilà, that’s it! The empty space for our button starts atcoordinate (0, 163) and has the dimensions (106, 53). The doneButtonmethod has to be written now of course, but that’s not hard any more.Just make sure that you call resignFirstResponder on the text fieldthat is being edited to have the keyboard slide down.
原码下载地址:http://files.neoos.ch/KeyboardExtension.zip
ely knowthat it lacks one very important feature: The UIKeyboardTypeNumberPaddoes not have a "return" key.
In fact every other keyboard type (except for the pretty similarUIKeyboardTypePhonePad) does offer the possibility to be dismissed bysetting the returnKeyType property of the correspondingUITextInputTraits implementor. So how does one achieve the same effectwith the number pad? We have found a workround!
When looking at the number pad, you'll notice that there is anunused space on its bottom left. That's where we are going to plug inour custom "return" key.
To make it short: take a screenshot, cut out the whole backspacekey, flip it horizotally, clear its backspace symbol in Photoshop andoverlay it with the text that we want on our “return” key. We’ve chosento label it “DONE”. Now we have the image for our custombutton’s UIControlStateNormal. Repeat the whole procedure (with atouched backspace key when taking the screenshot) to get a second imagefor our button’s UIControlStateHighlighted. Here’s the result:
Now back to coding. First we need to know when the number pad isgoing to be slided up on the screen so we can plug in our custom buttonbefore that happens. Luckily there’s a notification for exactly thatpurpose, and registering for it is as easy as:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[/pre]Don't forget to remove the observer from the notification center in the appropriate place once you're done with the whole thing:
[[NSNotificationCenter defaultCenter] removeObserver:self];[/pre]Now we’re getting to the heart of it. All we have to do in thekeyboardWillShow method is to locate the keyboard view and add ourbutton to it. The keyboard view is part of a second UIWindow of ourapplication as others have already figured out (see this thread).So we take a reference to that window (it will be the second window inmost cases, so objectAtIndex:1 in the code below is fine), traverse itsview hierarchy until we find the keyboard and add the button to itslower left:
- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}[/pre]Voilà, that’s it! The empty space for our button starts atcoordinate (0, 163) and has the dimensions (106, 53). The doneButtonmethod has to be written now of course, but that’s not hard any more.Just make sure that you call resignFirstResponder on the text fieldthat is being edited to have the keyboard slide down.
原码下载地址:http://files.neoos.ch/KeyboardExtension.zip
相关文章推荐
- 为UIKeyboardTypeNumberPad增加自定义按键
- 为UIKeyboardTypeNumberPad增加自定义按键
- 转:为UIKeyboardTypeNumberPad增加自定义按键
- 为UIKeyboardTypeNumberPad增加自定义按键
- iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键
- UIKeyboardTypeNumberPad 数字键盘自定义按键
- UIKeyboardTypeNumberPad 数字键盘自定义按键
- 设置键盘只能输入数字( phoneNumTextField.keyboardType = UIKeyboardTypeNumberPad;)
- UIKeyboardTypeNumberPad和丢失的return键
- UIKeyboardTypeNumberPad 数字键盘添加完成按钮
- Html Table用JS导出excel格式问题 导出EXCEL后单元格里的000412341234会变成412341234 7-14 会变成 2018-7-14(7月14) 自定义格式 web利用table表格生成excel格式问题 js导出excel增加表头、mso-number-format定义数据格式 数字输出格式转换 mso-number-format:"\@"
- 自定义系统apk 修改系统的一些属性值或者增加对HOME按键的监听的解决办法
- 【iOS知识学习】_iPhone学习基于UIKeyboardTypeDecimalPad(数字键盘+小数点)
- Android开发手机菜单(menu)按键的自定义
- iOS开发 自定义tableView样式(使用代码/使用Interface Builder)、分组显示、给TableView增加索引、给TableView增加SearchBar
- 如何使用自定义的 number range objects
- DB2 FUNCTION【DB2中增加自定义函数】
- gnome3增加自定义程序快捷方式
- 织梦dedecms栏目添加自定义字段,增加栏目上传缩略图功能
- select、input[type=number]在各大浏览器上隐藏右侧小箭头