为UIKeyboardTypeNumberPad增加自定义按键
2012-02-27 16:57
351 查看
在iphone上面(ipad上没这问题)把键盘设为NumberPad之后,会发现那个”return”键没有了,而这时候你又不想很土的在text field旁边自己加个按钮来做诸如完成输入/dismiss键盘之类的动作,其实这个是有解药的。
有没有发现最底下那行的左边有个空位,在那里补一个”return”键不就可以了么。这时候你需要这2个png(分别是doneup.png和donedown.png):
如果你是在iOS4上面写的话,你会需要这2个,因为iOS4上面的软键盘左下角没圆角:
接着就开始写代码来把贴有这个图片的UIButton在需要的时候加到那个空位那里了。幸好有专门的notification是帮你做这件事情的。所以在需要键盘出来之前,例如是点击UITextField的当下,往NSNotificationCenter注册一下这个notification就可以了。
?
在iphone 3.2 SDK之前,注册的是 UIKeyboardWillShowNotification,iOS4开始就要用 UIKeyboardDidShowNotification了。
然后在用完键盘之后要记得从NSNotificationCenter注销掉,不然你的别的软键盘都会粘上这个按钮了。
?
最后就是要在 addButtonToKeyboard 这个方法里面把keyboard
view找出来,把这个”DONE”的UIButton给粘上去了就完事了。前人已经找到了其实keyboard view就是在app的第二个UIWindow(看这里)。遍历一下这个UIWindow的孩子们,找到keyboard
view,然后把UIButton贴到左下角。需要注意的是iOS4的keyboard view的description跟iphone3.2之前的是不一样的噢。
?
最后把你想象里面按了这个”DONE”键会发生的事情例如resignFirstResponder 之类的,写在 doneButton 方法里面就完成了。
Posted
from Hangzhou, Zhejiang, China.
有没有发现最底下那行的左边有个空位,在那里补一个”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 ]; } |
然后在用完键盘之后要记得从NSNotificationCenter注销掉,不然你的别的软键盘都会粘上这个按钮了。
?
1 | [[ NSNotificationCenter defaultCenter] removeObserver: self ]; |
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.
分享到新浪微博 |
相关文章推荐
- 为UIKeyboardTypeNumberPad增加自定义按键
- 转:为UIKeyboardTypeNumberPad增加自定义按键
- 为UIKeyboardTypeNumberPad增加自定义按键
- 为UIKeyboardTypeNumberPad增加自定义按键
- UIKeyboardTypeNumberPad 数字键盘自定义按键
- iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键
- UIKeyboardTypeNumberPad 数字键盘自定义按键
- 设置键盘只能输入数字( phoneNumTextField.keyboardType = UIKeyboardTypeNumberPad;)
- UIKeyboardTypeNumberPad和丢失的return键
- UIKeyboardTypeNumberPad 数字键盘添加完成按钮
- 自定义系统apk 修改系统的一些属性值或者增加对HOME按键的监听的解决办法
- 【iOS知识学习】_iPhone学习基于UIKeyboardTypeDecimalPad(数字键盘+小数点)
- 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:"\@"
- Unity3D:给GameObject增加自定义变量
- 为你的linux增加自定义PATH
- 织梦搜索结果增加dede:type、dede:arclist、dede:channelartlist、dede:tag等标签支持
- Java基础---Java---IO流-----LineNumberReader方法及原理、自定义一个LineNumberReader、字节流、图片复制、mp3复制、
- Spring.Net -- 如何使用用户自定义类型转换器 (Custom Type Converter )
- 利用Hibernate自定义UserType简化编程
- WordPress RSS Feed中加入自定义Post Type的文章