ios toolbar 的自定义

iOS开发 键盘添加工具条(toolbar)

原文地址  http://gaohaijun.blog.163.com/blog/static/17669827120119233349519/

IOS 开发中,iphone/ipad/itouch采用的触摸设计,本身没有硬件键盘,一般都是点击输入框之后,弹出一个虚拟键盘出来,因此开发中,经常在完 成编辑输入之后,要写程序代码来关闭软键盘的输出,非常繁琐,当然关闭软键盘的方式有很多。本文要分享的是一个键盘顶部工具条的类,通过这个工具条,可以 很方便的关闭键盘,而且有上一项,下一项的输入框切换,非常方便,效果如下图所示:

 

ios toolbar 的自定义

一、KeyBoardTopBar类文件

  1)KeyBoardTopBar.h头文件

 

 

#import <Foundation/Foundation.h>
 
 
 @interface KeyBoardTopBar : NSObject {
 
 UIToolbar       *view;                       //工具条       
 
 NSArray         *textFields;                 //输入框数组
 
 BOOL            allowShowPreAndNext;         //是否显示上一项、下一项
 
 BOOL            isInNavigationController;    //是否在导航视图中
 
 UIBarButtonItem *prevButtonItem;             //上一项按钮
 
 UIBarButtonItem *nextButtonItem;             //下一项按钮
 
 UIBarButtonItem *hiddenButtonItem;           //隐藏按钮
 
 UIBarButtonItem *spaceButtonItem;            //空白按钮
 
 UITextField     *currentTextField;           //当前输入框
 
 }
 
 @property(nonatomic,retain) UIToolbar *view;
 
 
 -(id)init;
 
 -(void)setAllowShowPreAndNext:(BOOL)isShow;
 
 -(void)setIsInNavigationController:(BOOL)isbool;
 
 -(void)setTextFieldsArray:(NSArray *)array;
 
 -(void)showPrevious;
 
 -(void)showNext;
 
 -(void)showBar:(UITextField *)textField;
 
 -(void)HiddenKeyBoard;
 
 @end

 

2)KeyBoardTopBar.m实现文件

 #import "KeyBoardTopBar.h"
 
 
 @implementation KeyBoardTopBar
 
 @synthesize view;
 
 
 //初始化控件和变量
 
 -(id)init{
 
 if((self = [super init])) {
 
 prevButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"上一项" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowPrevious)];
 
 nextButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一项" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowNext)];
 
 hiddenButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏键盘" style:UIBarButtonItemStyleBordered target:self action:@selector(HiddenKeyBoard)];
 
 spaceButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 
 view = [[UIToolbar alloc] initWithFrame:CGRectMake(0,480,320,44)];
 
 view.barStyle = UIBarStyleBlackTranslucent;
 
 view.items = [NSArray arrayWithObjects:prevButtonItem,nextButtonItem,spaceButtonItem,hiddenButtonItem,nil];
 
 allowShowPreAndNext = YES;
 
 textFields = nil;
 
 isInNavigationController = YES;
 
 currentTextField = nil;
 
 }
 
 return self;
 
 }
 
 //设置是否在导航视图中
 
 -(void)setIsInNavigationController:(BOOL)isbool{
 
 isInNavigationController = isbool;
 
 }
 
 //显示上一项
 
 -(void)showPrevious{
 
 if (textFields==nil) {
 
 return;
 
 }
 
 NSInteger num = -1;
 
 for (NSInteger i=0; i<[textFields count]; i++) {
 
 if ([textFields objectAtIndex:i]==currentTextField) {
 
 num = i;
 
 break;
 
 }
 
 }
 
 if (num>0){
 
 [[textFields objectAtIndex:num] resignFirstResponder];
 
 [[textFields objectAtIndex:num-1 ] becomeFirstResponder];
 
 [self showBar:[textFields objectAtIndex:num-1]];
 
 }
 
 }
 
 //显示下一项
 
 -(void)showNext{
 
 if (textFields==nil) {
 
 return;
 
 }
 
 NSInteger num = -1;
 
 for (NSInteger i=0; i<[textFields count]; i++) {
 
 if ([textFields objectAtIndex:i]==currentTextField) {
 
 num = i;
 
 break;
 
 }
 
 }
 
 if (num<[textFields count]-1){
 
 [[textFields objectAtIndex:num] resignFirstResponder];
 
 [[textFields objectAtIndex:num+1] becomeFirstResponder];
 
 [self showBar:[textFields objectAtIndex:num+1]];
 
 }
 
 }
 
 //显示工具条
 
 -(void)showBar:(UITextField *)textField{
 
 currentTextField = textField;
 
 if (allowShowPreAndNext) {
 
 [view setItems:[NSArray arrayWithObjects:prevButtonItem,nextButtonItem,spaceButtonItem,hiddenButtonItem,nil]];
 
 }
 
 else {
 
 [view setItems:[NSArray arrayWithObjects:spaceButtonItem,hiddenButtonItem,nil]];
 
 }
 
 if (textFields==nil) {
 
 prevButtonItem.enabled = NO;
 
 nextButtonItem.enabled = NO;
 
 }
 
 else {
 
 NSInteger num = -1;
 
 for (NSInteger i=0; i<[textFields count]; i++) {
 
 if ([textFields objectAtIndex:i]==currentTextField) {
 
 num = i;
 
 break;
 
 }
 
 }
 
 if (num>0) {
 
 prevButtonItem.enabled = YES;
 
 }
 
 else {
 
 prevButtonItem.enabled = NO;
 
 }
 
 if (num<[textFields count]-1) {
 
 nextButtonItem.enabled = YES;
 
 }
 
 else {
 
 nextButtonItem.enabled = NO;
 
 }
 
 }
 
 [UIView beginAnimations:nil context:nil];
 
 [UIView setAnimationDuration:0.3];
 
 if (isInNavigationController) {
 
 view.frame = CGRectMake(0, 201-40, 320, 44);
 
 }
 
 else {
 
 view.frame = CGRectMake(0, 201, 320, 44);
 
 }
 
 [UIView commitAnimations];
 
 }
 
 //设置输入框数组
 
 -(void)setTextFieldsArray:(NSArray *)array{
 
 textFields = array;
 
 }
 
 //设置是否显示上一项和下一项按钮
 
 -(void)setAllowShowPreAndNext:(BOOL)isShow{
 
 allowShowPreAndNext = isShow;
 
 }
 
 //隐藏键盘和工具条
 
 -(void)HiddenKeyBoard{
 
 if (currentTextField!=nil) {
 
 [currentTextField  resignFirstResponder];
 
 }
 
 [UIView beginAnimations:nil context:nil];
 
 [UIView setAnimationDuration:0.3];
 
 view.frame = CGRectMake(0, 480, 320, 44);
 
 [UIView commitAnimations];
 
 }
 
 - (void)dealloc {
 
 [view release];
 
 [textFields release];
 
 [prevButtonItem release];
 
 [nextButtonItem release];
 
 [hiddenButtonItem release];
 
 [currentTextField release];
 
 [spaceButtonItem release];
 
     [super dealloc];
 
 }
 
 @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的实例对象调用显示键盘方法

 

}

 

 

 

三、总结

 

  上述的基本原理就是在键盘的上方,加一个透明状的工具条;当然不需要专门定义类,也可以的,可以直接在

 

需要调用键盘的地方加入工具条,让你的工具条随着键盘的出现而出现,消失而消失!

 

 

 

  大道至简,有更好的方法,希望一起讨论交流,谢谢!

 

你可能感兴趣的:(toolbar)