数字键盘添加自定义按钮(转自cocoachina)

项目需要对数字键盘做个性化设置,网上找了几个例子,学习了下,然后总结了一下:

 

数字键盘


 

身份证键盘

 



主要的代码如下
1.- (void)addButtonToKeyboardWithSelector:(SEL)sel normal:(UIImage*)nimg highlight:(UIImage*)himg{
2.     // create custom button
3.     UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
4.     doneButton.tag=8;
5.     doneButton.frame = CGRectMake(0, 0, 106, 53);
6.     doneButton.adjustsImageWhenHighlighted = NO; 
7.
8.     [doneButton setImage:nimg forState:UIControlStateNormal]; 
9.     [doneButton setImage:himg forState:UIControlStateHighlighted]; 
10.     [doneButton addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
11.     // locate keyboard view
12.     int cnt=[[UIApplication sharedApplication] windows].count;
13.     UIWindow* keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:cnt-1];           
14.     doneButton.frame = CGRectMake(0, keyboardWindow.frame.size.height-53, 106, 53);
15.     [keyboardWindow addSubview:doneButton];
16.
17.      NSLog(@"keyboard:%@ %@ %@",NSStringFromCGRect(keyboardWindow.frame),NSStringFromCGRect(doneButton.frame),keyboardWindow.subviews); 
18.} 
19.  
20.- (void)removeButtonFromKeyboard { 
21.    // locate keyboard view     
22.    int cnt=[[UIApplication sharedApplication] windows].count;
23.    UIWindow* keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:cnt-1];         23.24.[[keyboardWindow viewWithTag:8] removeFromSuperview]; 
25.} 

 
示例代码:   doneButton.zip (332 K)
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值