數字鍵盤添加自定義按鈕(轉自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)
 
發佈了12 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章