iOS模拟器键盘的下面添加一个完成按钮

来源:互联网 发布:u盘raw格式数据恢复 编辑:程序博客网 时间:2024/05/01 17:26

前言

为了满足一些用户的特殊需求,可能有的用户希望在键盘的左下角添加一个完成按钮,就像微信的右下角可以切换成"发送"和"换行"两个功能,所以我上网找了一些资料,整理了一下,大概的核心代码就是下面这些


  1. - (void)addDoneButtonToNumPadKeyboard  
  2. {  
  3.     UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];  
  4.     if (systemVersion < 8.0){  
  5.         doneButton.frame = CGRectMake(016310653);  
  6.     }else{  
  7.         doneButton.frame = CGRectMake(0, SCREEN_SIZE.height-5310653);  
  8.     }  
  9.     doneButton.tag = NUM_PAD_DONE_BUTTON_TAG;  
  10.     doneButton.adjustsImageWhenHighlighted = NO;  
  11.     [doneButton setTitle:@"完成" forState:UIControlStateNormal];  
  12.     [doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  13.     [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];  
  14.       
  15.     NSArray *windowArr = [[UIApplication sharedApplication] windows];  
  16.     if (windowArr != nil && windowArr.count > 1){  
  17.         UIWindow *needWindow = [windowArr objectAtIndex:1];  
  18.         UIView *keyboard;  
  19.         for(int i = 0; i < [needWindow.subviews count]; i++) {  
  20.             keyboard = [needWindow.subviews objectAtIndex:i];  
  21.             NSLog(@"%@", [keyboard description]);  
  22.             if(([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) || ([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) || ([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)){  
  23.                   
  24.                 UIView *doneButtonView = [keyboard viewWithTag:NUM_PAD_DONE_BUTTON_TAG];  
  25.                 if (doneButtonView == nil){  
  26.                     [keyboard addSubview:doneButton];  
  27.                 }  
  28.             }  
  29.         }  
  30.     }  
  31. }  
  32.   
  33. -(void)removeDoneButtonFromNumPadKeyboard  
  34. {  
  35.     UIView *doneButton = nil;  
  36.   
  37.     NSArray *windowArr = [[UIApplication sharedApplication] windows];  
  38.     if (windowArr != nil && windowArr.count > 1){  
  39.         UIWindow *needWindow = [windowArr objectAtIndex:1];  
  40.         UIView *keyboard;  
  41.         for(int i = 0; i < [needWindow.subviews count]; i++) {  
  42.             keyboard = [needWindow.subviews objectAtIndex:i];  
  43.             if(([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) || ([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) || ([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)){  
  44.                 doneButton = [keyboard viewWithTag:NUM_PAD_DONE_BUTTON_TAG];  
  45.                 if (doneButton != nil){  
  46.                     [doneButton removeFromSuperview];  
  47.                 }  
  48.             }  
  49.         }  
  50.     }  
  51. }  

0 0