原文地址 http://gaohaijun.blog.163.com/blog/static/17669827120119233349519/
IOS 开发中,iphone/ipad/itouch采用的触摸设计,本身没有硬件键盘,一般都是点击输入框之后,弹出一个虚拟键盘出来,因此开发中,经常在完 成编辑输入之后,要写程序代码来关闭软键盘的输出,非常繁琐,当然关闭软键盘的方式有很多。本文要分享的是一个键盘顶部工具条的类,通过这个工具条,可以 很方便的关闭键盘,而且有上一项,下一项的输入框切换,非常方便,效果如下图所示:
一、KeyBoardTopBar类文件
1)KeyBoardTopBar.h头文件
1 #import <Foundation/Foundation.h> 2 3 4 @interface KeyBoardTopBar : NSObject { 5 6 UIToolbar *view; //工具条 7 8 NSArray *textFields; //输入框数组 9 10 BOOL allowShowPreAndNext; //是否显示上一项、下一项 11 12 BOOL isInNavigationController; //是否在导航视图中 13 14 UIBarButtonItem *prevButtonItem; //上一项按钮 15 16 UIBarButtonItem *nextButtonItem; //下一项按钮 17 18 UIBarButtonItem *hiddenButtonItem; //隐藏按钮 19 20 UIBarButtonItem *spaceButtonItem; //空白按钮 21 22 UITextField *currentTextField; //当前输入框 23 24 } 25 26 @property(nonatomic,retain) UIToolbar *view; 27 28 29 -(id)init; 30 31 -(void)setAllowShowPreAndNext:(BOOL)isShow; 32 33 -(void)setIsInNavigationController:(BOOL)isbool; 34 35 -(void)setTextFieldsArray:(NSArray *)array; 36 37 -(void)showPrevious; 38 39 -(void)showNext; 40 41 -(void)showBar:(UITextField *)textField; 42 43 -(void)HiddenKeyBoard; 44 45 @end
2)KeyBoardTopBar.m实现文件
1 #import "KeyBoardTopBar.h" 2 3 4 @implementation KeyBoardTopBar 5 6 @synthesize view; 7 8 9 //初始化控件和变量 10 11 -(id)init{ 12 13 if((self = [super init])) { 14 15 prevButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"上一项" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowPrevious)]; 16 17 nextButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一项" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowNext)]; 18 19 hiddenButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏键盘" style:UIBarButtonItemStyleBordered target:self action:@selector(HiddenKeyBoard)]; 20 21 spaceButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 22 23 view = [[UIToolbar alloc] initWithFrame:CGRectMake(0,480,320,44)]; 24 25 view.barStyle = UIBarStyleBlackTranslucent; 26 27 view.items = [NSArray arrayWithObjects:prevButtonItem,nextButtonItem,spaceButtonItem,hiddenButtonItem,nil]; 28 29 allowShowPreAndNext = YES; 30 31 textFields = nil; 32 33 isInNavigationController = YES; 34 35 currentTextField = nil; 36 37 } 38 39 return self; 40 41 } 42 43 //设置是否在导航视图中 44 45 -(void)setIsInNavigationController:(BOOL)isbool{ 46 47 isInNavigationController = isbool; 48 49 } 50 51 //显示上一项 52 53 -(void)showPrevious{ 54 55 if (textFields==nil) { 56 57 return; 58 59 } 60 61 NSInteger num = -1; 62 63 for (NSInteger i=0; i<[textFields count]; i++) { 64 65 if ([textFields objectAtIndex:i]==currentTextField) { 66 67 num = i; 68 69 break; 70 71 } 72 73 } 74 75 if (num>0){ 76 77 [[textFields objectAtIndex:num] resignFirstResponder]; 78 79 [[textFields objectAtIndex:num-1 ] becomeFirstResponder]; 80 81 [self showBar:[textFields objectAtIndex:num-1]]; 82 83 } 84 85 } 86 87 //显示下一项 88 89 -(void)showNext{ 90 91 if (textFields==nil) { 92 93 return; 94 95 } 96 97 NSInteger num = -1; 98 99 for (NSInteger i=0; i<[textFields count]; i++) { 100 101 if ([textFields objectAtIndex:i]==currentTextField) { 102 103 num = i; 104 105 break; 106 107 } 108 109 } 110 111 if (num<[textFields count]-1){ 112 113 [[textFields objectAtIndex:num] resignFirstResponder]; 114 115 [[textFields objectAtIndex:num+1] becomeFirstResponder]; 116 117 [self showBar:[textFields objectAtIndex:num+1]]; 118 119 } 120 121 } 122 123 //显示工具条 124 125 -(void)showBar:(UITextField *)textField{ 126 127 currentTextField = textField; 128 129 if (allowShowPreAndNext) { 130 131 [view setItems:[NSArray arrayWithObjects:prevButtonItem,nextButtonItem,spaceButtonItem,hiddenButtonItem,nil]]; 132 133 } 134 135 else { 136 137 [view setItems:[NSArray arrayWithObjects:spaceButtonItem,hiddenButtonItem,nil]]; 138 139 } 140 141 if (textFields==nil) { 142 143 prevButtonItem.enabled = NO; 144 145 nextButtonItem.enabled = NO; 146 147 } 148 149 else { 150 151 NSInteger num = -1; 152 153 for (NSInteger i=0; i<[textFields count]; i++) { 154 155 if ([textFields objectAtIndex:i]==currentTextField) { 156 157 num = i; 158 159 break; 160 161 } 162 163 } 164 165 if (num>0) { 166 167 prevButtonItem.enabled = YES; 168 169 } 170 171 else { 172 173 prevButtonItem.enabled = NO; 174 175 } 176 177 if (num<[textFields count]-1) { 178 179 nextButtonItem.enabled = YES; 180 181 } 182 183 else { 184 185 nextButtonItem.enabled = NO; 186 187 } 188 189 } 190 191 [UIView beginAnimations:nil context:nil]; 192 193 [UIView setAnimationDuration:0.3]; 194 195 if (isInNavigationController) { 196 197 view.frame = CGRectMake(0, 201-40, 320, 44); 198 199 } 200 201 else { 202 203 view.frame = CGRectMake(0, 201, 320, 44); 204 205 } 206 207 [UIView commitAnimations]; 208 209 } 210 211 //设置输入框数组 212 213 -(void)setTextFieldsArray:(NSArray *)array{ 214 215 textFields = array; 216 217 } 218 219 //设置是否显示上一项和下一项按钮 220 221 -(void)setAllowShowPreAndNext:(BOOL)isShow{ 222 223 allowShowPreAndNext = isShow; 224 225 } 226 227 //隐藏键盘和工具条 228 229 -(void)HiddenKeyBoard{ 230 231 if (currentTextField!=nil) { 232 233 [currentTextField resignFirstResponder]; 234 235 } 236 237 [UIView beginAnimations:nil context:nil]; 238 239 [UIView setAnimationDuration:0.3]; 240 241 view.frame = CGRectMake(0, 480, 320, 44); 242 243 [UIView commitAnimations]; 244 245 } 246 247 - (void)dealloc { 248 249 [view release]; 250 251 [textFields release]; 252 253 [prevButtonItem release]; 254 255 [nextButtonItem release]; 256 257 [hiddenButtonItem release]; 258 259 [currentTextField release]; 260 261 [spaceButtonItem release]; 262 263 [super dealloc]; 264 265 } 266 267 @end
二、如何使用
1)在用到输入的controller类.h头文件中,首先引入导入KeyBoardTopBar:
#import "KeyBoardTopBar.h"
NSMutableArray *editFieldArray; //存放视图中可编辑的控件
KeyBoardTopBar *keyboardbar;
2)在用到输入的controller类.m实现文件中,首先初始化KeyBoardTopBar类的实例,如下:
keyboardbar = [[KeyBoardTopBar alloc]init];
[keyboardbar setAllowShowPreAndNext:YES];
[keyboardbar setIsInNavigationController:NO];
[keyboardbar setTextFieldsArray:editFieldArray];
[self.view addSubview:keyboardbar.view];
3)在用到输入的controller类.m实现文件中,在此以UITextField为例说明,在它的回调方法中,实现如下:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[keyboardbar showBar:textField];//KeyBoardTopBar的实例对象调用显示键盘方法
}
三、总结
上述的基本原理就是在键盘的上方,加一个透明状的工具条;当然不需要专门定义类,也可以的,可以直接在
需要调用键盘的地方加入工具条,让你的工具条随着键盘的出现而出现,消失而消失!
大道至简,有更好的方法,希望一起讨论交流,谢谢!