为UIKeyboardTypeNumberPad增加自定义按键

在iphone上面(ipad上没这问题)把键盘设为NumberPad之后,会发现那个”return”键没有了,而这时候你又不想很土的在text field旁边自己加个按钮来做诸如完成输入/dismiss键盘之类的动作,其实这个是有解药的。

有没有发现最底下那行的左边有个空位,在那里补一个”return”键不就可以了么。这时候你需要这2个png(分别是doneup.png和donedown.png):

 

如果你是在iOS4上面写的话,你会需要这2个,因为iOS4上面的软键盘左下角没圆角:

 

接着就开始写代码来把贴有这个图片的UIButton在需要的时候加到那个空位那里了。幸好有专门的notification是帮你做这件事情的。所以在需要键盘出来之前,例如是点击UITextField的当下,往NSNotificationCenter注册一下这个notification就可以了。

?
01
02
03
04
05
06
07
08
09
10
11
12
13
// add observer for the respective notifications
// (depending on the os version)
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
     [[ NSNotificationCenter defaultCenter] addObserver: self
                       selector: @selector (addButtonToKeyboard)
                         name:UIKeyboardDidShowNotification
                                                object: nil ];
} else {
     [[ NSNotificationCenter defaultCenter] addObserver: self
                       selector: @selector (addButtonToKeyboard)
                         name:UIKeyboardWillShowNotification
                                                object: nil ];
}

在iphone 3.2 SDK之前,注册的是 UIKeyboardWillShowNotification,iOS4开始就要用 UIKeyboardDidShowNotification了。

然后在用完键盘之后要记得从NSNotificationCenter注销掉,不然你的别的软键盘都会粘上这个按钮了。

?
1
[[ NSNotificationCenter defaultCenter] removeObserver: self ];

最后就是要在 addButtonToKeyboard 这个方法里面把keyboard view找出来,把这个”DONE”的UIButton给粘上去了就完事了。前人已经找到了其实keyboard view就是在app的第二个UIWindow(看这里)。遍历一下这个UIWindow的孩子们,找到keyboard view,然后把UIButton贴到左下角。需要注意的是iOS4的keyboard view的description跟iphone3.2之前的是不一样的噢。

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
- ( void )addButtonToKeyboard {
     // create custom button
     UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
     doneButton.frame = CGRectMake(0, 163, 106, 53);
     doneButton.adjustsImageWhenHighlighted = NO ;
     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
         [doneButton setImage:[UIImage imageNamed: @"DoneUp3.png" ]
                             forState:UIControlStateNormal];
         [doneButton setImage:[UIImage imageNamed: @"DoneDown3.png" ]
                             forState:UIControlStateHighlighted];
     } else {
         [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 found, add the button
         if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
             if ([[keyboard description] hasPrefix: @"<UIPeripheralHost" ] == YES )
                 [keyboard addSubview:doneButton];
         } else {
             if ([[keyboard description] hasPrefix: @"<UIKeyboard" ] == YES )
                 [keyboard addSubview:doneButton];
         }
     }
}

最后把你想象里面按了这个”DONE”键会发生的事情例如resignFirstResponder 之类的,写在 doneButton 方法里面就完成了。



Posted from Hangzhou, Zhejiang, China.

  分享到新浪微博
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值