为数字键盘添加自定义按钮的方法
2014-08-13 11:59
615 查看
为数字键盘添加自定义按钮的方法(iOS所有版本适用)
因为的UIKeyboardTypeNumberPad类型的键盘没有完成按钮,为了自己加这个按钮,网上有N种方法,大体的思路就是:在获得键盘弹出通知时,在键盘的那个UIView上添加一个自定义的UIButton,但是这些方法都存在问题:1.使用UIKeyboardWillShowNotification时,我在iphone4.3及5.0模拟器上发现keyboard视图还没有创建出来
2.ios5上,要根据不同的uiview description来找到keyboard,版本不同descruption不同
3.ios5上,添加UIButton的位置不同(x,y坐标都为负数才能显示按钮),并且按钮加上后不能被点击
折腾一上午,终于找到了方法,很简单,不用去找那个keyboard了,直接在keyboard的那个window上加button,直接就搞定了ios的所有版本。
方法如下:
1. 先注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
2. 在dealloc中反注册通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
3.实现通知处理
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
if (doneInKeyboardButton.superview)
{
[doneInKeyboardButton removeFromSuperview];
}
}
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
normalKeyboardHeight = kbSize.height;
// create custom button
if (doneInKeyboardButton == nil)
{
doneInKeyboardButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
doneInKeyboardButton.frame = CGRectMake(0, 480 - 53, 106, 53);
doneInKeyboardButton.adjustsImageWhenHighlighted = NO;
[doneInKeyboardButton setImage:[UIImage imageNamed:@"numPadDoneUp.png"] forState:UIControlStateNormal];
[doneInKeyboardButton setImage:[UIImage imageNamed:@"numPadDoneDown.png"] forState:UIControlStateHighlighted];
[doneInKeyboardButton addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
}
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
if (doneInKeyboardButton.superview == nil)
{
[tempWindow addSubview:doneInKeyboardButton]; // 注意这里直接加到window上
}
}
相关文章推荐
- 为数字键盘添加自定义按钮的方法(iOS所有版本适用
- 数字键盘添加自定义按钮(转自cocoachina)
- 数字键盘添加自定义按钮
- 数字键盘添加自定义按钮
- iOS8 数字键盘添加自定义按钮(兼容各个版本以及解决自定义按钮滑动出现的问题)
- IOS数字键盘左下角添加完成按钮的实现方法
- 数字键盘添加“完成”按钮
- iOS8数字键盘左下角添加完成按钮
- Kotlin自定义实现支付密码数字键盘的方法实例
- ios隐藏数字键盘 --在数字键盘左下角添加“完成”按钮的实现原理
- iOS8数字键盘左下角添加完成按钮的核心代码如下:
- (判断第三方输入法)在系统数字键盘上添加“完成”按钮引发的问题思考
- 给FCKeditor添加自定义按钮的方法
- UIKeyboardTypeNumberPad 数字键盘添加完成按钮
- iOS 数字输入键盘添加一个完成按钮
- ios在数字键盘左下角添加“完成”按钮的实现原理
- 第十三篇、Swift_Nav自定义返回按钮后或者隐藏导航栏,Pop返回手势失效的解决方法 Pop全局返回添加的方法
- ios在数字键盘左下角添加“完成”按钮的实现原理
- 1怎样让输入的text文本填写的部分只添加数字、2怎样将输入的数字一次性全部删除 3怎样选择密码 让它不显示 4 怎样实现在没有return的情况下点击button的情况下就能够使键盘页面下落5 怎样实现点击指定的button来对使指定的text进行键盘弹出 6怎样改变button按钮的状态
- FCKeditor添加自定义按钮的方法