ios数字键盘添加完成按钮,示代码如下(附件中有效果图和示例工程代码):
// // ViewController.h // KeyboardTest // 自定义数字键盘,添加完成按钮 // Created by Dwen on 12-10-24. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController //定义完成按钮 @property (weak,nonatomic) UIButton *doneInKeyboardButton; //普通键盘 - (IBAction)normalKeyboard:(id)sender; //自定义键盘 - (IBAction)defineKeyboard:(id)sender; @end
// // ViewController.m // KeyboardTest // // Created by on 12-10-24. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize doneInKeyboardButton; - (void)viewDidLoad { [super viewDidLoad]; //注册键盘显示通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keywordboardShow) name:UIKeyboardDidShowNotification object:nil]; //注册键盘隐藏通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keywordboardHide) name:UIKeyboardDidHideNotification object:nil]; } //键盘显示 - (void)keywordboardShow{ if (doneInKeyboardButton == nil){ //初始化完成按钮 doneInKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom]; //获取主屏高度 CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height; if(screenHeight==568.0f){//iphone5 doneInKeyboardButton.frame = CGRectMake(0, 568 - 53, 106, 53); }else{//iphone4 3.5寸 doneInKeyboardButton.frame = CGRectMake(0, 480 - 53, 106, 53); } //在按钮被禁用时,图像会被画的颜色深一些 doneInKeyboardButton.adjustsImageWhenHighlighted = NO; //根据按钮不同状态设图片 [doneInKeyboardButton setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal]; [doneInKeyboardButton setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateHighlighted]; [doneInKeyboardButton addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside]; } UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; if (doneInKeyboardButton.superview == nil){ //完成按钮添加到window [tempWindow addSubview:doneInKeyboardButton]; } } //键盘隐藏 - (void)keywordboardHide{ if (doneInKeyboardButton.superview){ //从视图中移除掉 [doneInKeyboardButton removeFromSuperview]; } } //点击完成按键 -(void)finishAction{ //隐藏完成按钮 doneInKeyboardButton.hidden = YES; [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘 } #pragma 文本 //普通键盘 - (IBAction)normalKeyboard:(id)sender { doneInKeyboardButton.hidden = NO; } //自定义键盘 - (IBAction)defineKeyboard:(id)sender { doneInKeyboardButton.hidden = YES; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } @end