【在数字键盘上添加button】
来源:互联网 发布:网红淘宝店质量 编辑:程序博客网 时间:2024/04/27 15:17
一、遍历添加
//定义一个消息中心
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //addObserver:注册一个观察员 name:消息名称- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
[doneButton setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(addRadixPoint) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];//返回应用程序window
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) //遍历window上的所有subview
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
二、直接添加到window上
因为的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上
}
}
这样就搞定了,是不是很简单?总体上还是那个思路,但网上的方法细节上有些误导。
- 在数字键盘上添加button
- 【在数字键盘上添加button】
- 如何在Button上添加图片
- (判断第三方输入法)在系统数字键盘上添加“完成”按钮引发的问题思考
- button上添加文字
- UITableView 上添加button
- 如何在你的IE上添加一个BUTTON
- 关于在navigationItem.titleView上添加button不显示问题
- xib方式 在tableviewcell上添加button (代理方式)
- 在ScrollView上添加button谁先响应?
- Android中在Button文本上添加图片
- 关于在ImageView上添加Button,但是点击没反应
- ios隐藏数字键盘 --在数字键盘左下角添加“完成”按钮的实现原理
- ios在数字键盘左下角添加“完成”按钮的实现原理
- 自定义数字键盘(固定button键盘)
- 数字键盘添加自定义按钮
- 数字键盘添加自定义按钮
- 数字键盘添加自定义按钮
- 8x25 平台上添加虚拟按键
- 麻烦死了
- POJ 1555
- 可我仍然为自己违背了父亲多年的教诲而感到内疚自责
- C++的函数重载
- 【在数字键盘上添加button】
- Android 链接https出现 javax.net.ssl.SSLException: hostname in certificate didn't match:
- 在线广告基本概念小记<转>
- Windows 错误报告
- 2014-05-13MFC对话框:改变MFC中按钮的文字,更改窗体的大小,通过类名能够查找到该类对应的对话框,MFC中获取文本框中的内容,updatedata函数,两种对话框比较,oncreate起点
- svn服务器端回退版本
- oracle job 定时执行 存储过程
- 数据适配器对象和数据集对象(一般用来执行查询的动作,例如:查看表中的所有数据并显示在主窗体中)
- spring的bean管理