ios 数字键盘左下角添加按钮_ios数字键盘添加完成按钮

ios数字键盘添加完成按钮,示代码如下(附件中有效果图和示例工程代码):

//

// ViewController.h

// KeyboardTest

// 自定义数字键盘,添加完成按钮

// Created by Dwen on 12-10-24.

// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.

//

#import

@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:@"btn_done_up@2x.png"] forState:UIControlStateNormal];

[doneInKeyboardButton setImage:[UIImage imageNamed:@"btn_done_down@2x.png"] 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值