codecamp

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

iOS8数字键盘左下角添加完成按钮的核心代码如下:

  1. - (void)addDoneButtonToNumPadKeyboard  
  2. {  
  3.     UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];  
  4.     if (systemVersion < 8.0){  
  5.         doneButton.frame = CGRectMake(0, 163, 106, 53);  
  6.     }else{  
  7.         doneButton.frame = CGRectMake(0, SCREEN_SIZE.height-53, 106, 53);  
  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. }  





注:

1.iOS8之后,键盘view的description变为以<UIInputSetContainerView开头,所以需要在判断的逻辑中加上这一项。

2.iOS8之后,键盘view的大小变成了整个屏幕的大小,所以需要对按钮添加的位置进行适配。



xcode6模拟器模拟位置功能
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }