注册 登录
主题 : 为UIKeyboardTypeNumberPad增加自定义按键
级别: 圣骑士

UID: 2229
精华: 1
发帖: 71
可可豆: 6505 CB
威望: 6505 点
在线时间: 388(时)
注册时间: 2009-02-16
最后登录: 2010-02-12
0 楼:  发表于: 2009-05-06 11:23    发自: Web Page

为UIKeyboardTypeNumberPad增加自定义按键   

这篇英文较简单,就懒得翻译了。

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://files.neoos.ch/KeyboardExtension.zip
清空我的评分动态本帖最近评分记录: 共可可豆条评分记录
gagaga 贡献值 +5 2008-09-19
隐藏评分记录
级别: 侠客
UID: 1529
精华: 0
发帖: 27
可可豆: 1488 CB
威望: 1488 点
在线时间: 568(时)
注册时间: 2008-12-03
最后登录: 2015-08-16
1 楼:  发表于: 2009-05-06 11:36    发自: Web Page
感谢,这个很有用
级别: 侠客
UID: 1644
精华: 0
发帖: 8
可可豆: 1030 CB
威望: 1030 点
在线时间: 250(时)
注册时间: 2008-12-16
最后登录: 2015-04-25
2 楼:  发表于: 2009-05-06 20:20    发自: Web Page
很牛的代码
just so so
级别: 侠客
UID: 644
精华: 0
发帖: 60
可可豆: 1602 CB
威望: 1602 点
在线时间: 409(时)
注册时间: 2008-09-08
最后登录: 2011-05-12
3 楼:  发表于: 2009-05-06 20:28    发自: Web Page
很有才的LZ
.........................................
级别: 圣骑士

UID: 2229
精华: 1
发帖: 71
可可豆: 6505 CB
威望: 6505 点
在线时间: 388(时)
注册时间: 2009-02-16
最后登录: 2010-02-12
4 楼:  发表于: 2009-05-07 00:21    发自: Web Page
引用
引用第3楼yrh1234于2009-05-06 20:28发表的  :
很有才的LZ
.........................................


不是我的原创代码,我只是转载。
级别: 版主

状态: 连续签到 - [144天]
UID: 491
精华: 6
发帖: 1766
可可豆: 112205 CB
威望: 112852 点
在线时间: 8537(时)
注册时间: 2008-08-19
最后登录: 2019-02-28
5 楼:  发表于: 2009-05-07 00:50    发自: Web Page
局限性很大
引用
引用第4楼alan于2009-05-07 12:21 AM发表的  :


不是我的原创代码,我只是转载。

 Developer
------------------------------------------------------------
Η αγάπη ποτέ δεν αποτυγχάνει.
愛是永不止息。
Love never fails.

    --《圣经.新约》哥林多前书第13章
级别: 新手上路
状态: 连续签到 - [1天]
UID: 6924
精华: 0
发帖: 3
可可豆: 220 CB
威望: 206 点
在线时间: 84(时)
注册时间: 2009-07-10
最后登录: 2016-11-18
6 楼:  发表于: 2009-07-10 14:58    发自: Web Page
不知道 输入资料键盘可否自订呢?
意思就是 整个键盘都自己做 大小跟图片等 似乎工程浩大..  
由于横屏下键盘挡住大部分画面 想设法解决
级别: 骑士

UID: 1439
精华: 1
发帖: 41
可可豆: 5194 CB
威望: 5154 点
在线时间: 1788(时)
注册时间: 2008-11-26
最后登录: 2017-06-01
7 楼:  发表于: 2009-07-10 15:05    发自: Web Page
Done是一张图片,活活活
如果你有空,可以找我聊天。
http://www.cocoachina.com/bbs/?u=1439
级别: 禁止发言

状态: 连续签到 - [1天]
UID: 147
精华: 6
发帖: 562
可可豆: 17848 CB
威望: 17837 点
在线时间: 951(时)
注册时间: 2008-06-13
最后登录: 2018-09-19
8 楼:  发表于: 2009-07-10 15:20    发自: Web Page
用户被禁言,该主题自动屏蔽!
级别: 新手上路
状态: 连续签到 - [1天]
UID: 6924
精华: 0
发帖: 3
可可豆: 220 CB
威望: 206 点
在线时间: 84(时)
注册时间: 2009-07-10
最后登录: 2016-11-18
9 楼:  发表于: 2009-07-12 00:29    发自: Web Page
引用
引用第6楼kyle77212于2009-07-10 14:58发表的  :
不知道 输入资料键盘可否自订呢?
意思就是 整个键盘都自己做 大小跟图片等 似乎工程浩大..  
由于横屏下键盘挡住大部分画面 想设法解决

自頂一個 希望有人能幫幫我 感恩

CocoaChina社区转载内容已尽可能注明出处,如未能核实来源或转发内容图片有权利瑕疵的,请及时联系社区进行修改或删除【联系方式QQ : 3442093904 邮箱:support@cocoachina.com】文章内容为作者独立观点,不代表CocoaChina社区立场。版权归原作者所有,如申请授权请联系作者,因文章侵权CocoaChina社区不承担任何法律及连带责任。

描述
快速回复

关注本帖(如果有新回复会站内信通知您)

发帖、回帖都会得到可观的积分奖励。查看论坛积分规则

按"Ctrl+Enter"直接提交
    顶部