iOS自定义数字键盘
2017-01-06 11:15
309 查看
因为的UIKeyboardTypeNumberPad类型的键盘没有完成按钮,为了自己加这个按钮,网上有N种方法,大体的思路就是:在获得键盘弹出通知时,在键盘的那个UIView上添加一个自定义的UIButton,但是这些方法都存在问题:
1.使用UIKeyboardWillShowNotification时,我在iphone4.3及5.0模拟器上发现keyboard视图还没有创建出来
2.ios5上,要根据不同的uiview description来找到keyboard,版本不同descruption不同
3.ios5上,添加UIButton的位置不同(x,y坐标都为负数才能显示按钮),并且按钮加上后不能被点击
#define KEY_WIDTH 106
#define KEY_HEIGHT 53
#pragma mark - 处理TextField响应事件
- (void)editingDidBegin:(UITextField *)textF {
[self.textFbecomeFirstResponder];
}
//3.实现通知处理
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
if (doneInKeyboardButton.superview)
{
[doneInKeyboardButtonremoveFromSuperview];
}
}
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
NSDictionary *info = [notificationuserInfo];
CGSize kbSize = [[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
CGFloat normalKeyboardHeight = kbSize.height;
int cnt = [[UIApplicationsharedApplication]
windows].count;
UIWindow* tempWindow = [[[UIApplicationsharedApplication]
windows] objectAtIndex:cnt-1];
// create custom button
if (doneInKeyboardButton ==nil)
{
doneInKeyboardButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
doneInKeyboardButton.frame =CGRectMake(18,
tempWindow.frame.size.height-53,106,
53);
doneInKeyboardButton.adjustsImageWhenHighlighted =NO;
[doneInKeyboardButtonsetImage:[UIImageimageNamed:@"done.png"]forState:UIControlStateNormal];
[doneInKeyboardButtonsetImage:[UIImageimageNamed:@"done.png"]forState:UIControlStateHighlighted];
[doneInKeyboardButtonaddTarget:selfaction:@selector(finishAction)forControlEvents:UIControlEventTouchUpInside];
}
// locate keyboard view
if (doneInKeyboardButton.superview
== nil)
{
// 注意这里直接加到window上
[tempWindow addSubview:doneInKeyboardButton];
}
}
#pragma mark - 处理视图响应事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.textFresignFirstResponder];
}
-(void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
//1. 先注册通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleKeyboardDidShow:)name:UIKeyboardDidShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleKeyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];
}
//2. 在dealloc中反注册通知
-(void)dealloc
{
[[NSNotificationCenterdefaultCenter]
removeObserver:self];
}
1.使用UIKeyboardWillShowNotification时,我在iphone4.3及5.0模拟器上发现keyboard视图还没有创建出来
2.ios5上,要根据不同的uiview description来找到keyboard,版本不同descruption不同
3.ios5上,添加UIButton的位置不同(x,y坐标都为负数才能显示按钮),并且按钮加上后不能被点击
#define KEY_WIDTH 106
#define KEY_HEIGHT 53
#pragma mark - 处理TextField响应事件
- (void)editingDidBegin:(UITextField *)textF {
[self.textFbecomeFirstResponder];
}
//3.实现通知处理
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
if (doneInKeyboardButton.superview)
{
[doneInKeyboardButtonremoveFromSuperview];
}
}
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
NSDictionary *info = [notificationuserInfo];
CGSize kbSize = [[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
CGFloat normalKeyboardHeight = kbSize.height;
int cnt = [[UIApplicationsharedApplication]
windows].count;
UIWindow* tempWindow = [[[UIApplicationsharedApplication]
windows] objectAtIndex:cnt-1];
// create custom button
if (doneInKeyboardButton ==nil)
{
doneInKeyboardButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
doneInKeyboardButton.frame =CGRectMake(18,
tempWindow.frame.size.height-53,106,
53);
doneInKeyboardButton.adjustsImageWhenHighlighted =NO;
[doneInKeyboardButtonsetImage:[UIImageimageNamed:@"done.png"]forState:UIControlStateNormal];
[doneInKeyboardButtonsetImage:[UIImageimageNamed:@"done.png"]forState:UIControlStateHighlighted];
[doneInKeyboardButtonaddTarget:selfaction:@selector(finishAction)forControlEvents:UIControlEventTouchUpInside];
}
// locate keyboard view
if (doneInKeyboardButton.superview
== nil)
{
// 注意这里直接加到window上
[tempWindow addSubview:doneInKeyboardButton];
}
}
#pragma mark - 处理视图响应事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.textFresignFirstResponder];
}
-(void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
//1. 先注册通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleKeyboardDidShow:)name:UIKeyboardDidShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleKeyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];
}
//2. 在dealloc中反注册通知
-(void)dealloc
{
[[NSNotificationCenterdefaultCenter]
removeObserver:self];
}
相关文章推荐
- iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键
- IOS自定义的数字键盘 FPNumberPadView
- IOS 自定义简陋的数字键盘
- ios中如何自定义数字键盘
- iOS开发:自定义数字键盘(两种方式)
- iOS开发之自定义键盘(数字,字母类型等随意切换)
- ios自定义数字键盘
- iOS数字键盘自定义按键
- 为数字键盘添加自定义按钮的方法(iOS所有版本适用
- iOS开发系列之常用自定义控件开发集—自定义数字键盘控件开发
- ios键盘通知和自定义键盘
- IOS开发之自定义系统弹出键盘上方的view
- 为数字键盘添加自定义按钮的方法
- ios自定义键盘高度
- ios之键盘的自定义
- Android-自定义数字小键盘
- IOS自定义键盘
- ios键盘通知和自定义键盘
- 自定义悬浮在界面上的数字键盘
- IOS开发之自定义系统弹出键盘上方的view(转载)