转:为UIKeyboardTypeNumberPad增加自定义按键
2011-05-27 16:41
405 查看
这篇英文较简单,就懒得翻译了。
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 surely 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://www.cocoachina.com/bbs/simple/?t3999.html
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 surely 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://www.cocoachina.com/bbs/simple/?t3999.html
相关文章推荐
- 为UIKeyboardTypeNumberPad增加自定义按键
- 为UIKeyboardTypeNumberPad增加自定义按键
- 为UIKeyboardTypeNumberPad增加自定义按键
- 为UIKeyboardTypeNumberPad增加自定义按键
- UIKeyboardTypeNumberPad 数字键盘自定义按键
- iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键
- UIKeyboardTypeNumberPad 数字键盘自定义按键
- 设置键盘只能输入数字( phoneNumTextField.keyboardType = UIKeyboardTypeNumberPad;)
- UIKeyboardTypeNumberPad和丢失的return键
- UIKeyboardTypeNumberPad 数字键盘添加完成按钮
- 自定义系统apk 修改系统的一些属性值或者增加对HOME按键的监听的解决办法
- 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:"\@"
- 【iOS知识学习】_iPhone学习基于UIKeyboardTypeDecimalPad(数字键盘+小数点)
- A10开发板增加红外驱动及android层红外按键修改介绍
- 自定义可以按键控制自由移动的View
- 移除HTML5 input在type="number"时的上下小箭头
- beego增加用户自定义模板函数,index从1开始
- spring boot 项目中hanlp的配置(可增加自定义词典)
- 自定义input[type="radio"]的样式
- 去掉input标签type="number" 的默认格式