自定义键盘上的按钮

时间:2023-02-19 23:47:27

本文摘自http://blog.csdn.net/hitwhylz/article/details/29263027


自定义键盘

源码可以到我的github中下载:https://github.com/colin1994/myKeyboard.git

可以通过自定义键盘, 在键盘上加入你需要的功能, 即可。

效果如下:

自定义键盘上的按钮


代码如下:

[cpp]  view plain copy 自定义键盘上的按钮 自定义键盘上的按钮
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.       
  6.       
  7.       
  8.     if (self.keyboardToolbar == nil)  
  9.     {  
  10.         self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 38.0f)];  
  11.         self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent;  
  12.           
  13.         UIBarButtonItem *previousBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"前进", @"")  
  14.                                                                             style:UIBarButtonItemStyleBordered  
  15.                                                                            target:self  
  16.                                                                            action:@selector(previousField:)];  
  17.           
  18.         UIBarButtonItem *nextBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"后退", @"")  
  19.                                                                         style:UIBarButtonItemStyleBordered  
  20.                                                                        target:self  
  21.                                                                        action:@selector(nextField:)];  
  22.           
  23.         UIBarButtonItem *spaceBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  
  24.                                                                                       target:nil  
  25.                                                                                       action:nil];  
  26.           
  27.         UIBarButtonItem *doneBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"隐藏", @"")  
  28.                                                                         style:UIBarButtonItemStyleDone  
  29.                                                                        target:self  
  30.                                                                        action:@selector(resignKeyboard:)];  
  31.           
  32.         [self.keyboardToolbar setItems:[NSArray arrayWithObjects:previousBarItem, nextBarItem, spaceBarItem, doneBarItem, nil]];  
  33.     }  
  34.       
  35.     self.myTextView.inputAccessoryView = self.keyboardToolbar;  
  36.   
  37. }  
  38.   
  39.   
  40. #pragma mark - your code  
  41. - (void)resignKeyboard:(id)sender  
  42. {  
  43.     [self.myTextView resignFirstResponder];  
  44. }  
  45.   
  46. - (void)previousField:(id)sender  
  47. {  
  48.   
  49. }  
  50.   
  51. - (void)nextField:(id)sender  
  52. {  
  53.   
  54. }