// 工具條
// HMKeyboardTool.h
#import <UIKit/UIKit.h>
typedef enum {
KeyboardItemTypePrevious,//上一個
KeyboardItemTypeNext,//下一個
KeyboardItemTypeDone//完成
}KeyboardItemType;
@class HMKeyboardTool;
@protocol HMKeyboardToolDelegate <NSObject>
-(void)keyboardTool:(HMKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType;
@end
@interface HMKeyboardTool : UIView
//添加代理
@property(nonatomic,weak)id<HMKeyboardToolDelegate> delgate;
+ (instancetype)keyboardTool;
@end
// HMKeyboardTool.m
#import "HMKeyboardTool.h"
@interface HMKeyboardTool()
- (IBAction)previous:(id)sender;
- (IBAction)next:(id)sender;
- (IBAction)done:(id)sender;
@end
@implementation HMKeyboardTool
+ (instancetype)keyboardTool{
return [[[NSBundle mainBundle] loadNibNamed:@"HMKeyboardTool" owner:nil options:nil] lastObject];
}
//上一個
- (IBAction)previous:(id)sender {
//判斷代理有沒有實(shí)現(xiàn)相應(yīng)的方法
if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
[self.delgate keyboardTool:self didClickItemType:KeyboardItemTypePrevious];
}
}
//下一個
- (IBAction)next:(id)sender {
//判斷代理有沒有實(shí)現(xiàn)相應(yīng)的方法
if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
[self.delgate keyboardTool:self didClickItemType:KeyboardItemTypeNext];
}
}
//完成
- (IBAction)done:(id)sender {
//判斷代理有沒有實(shí)現(xiàn)相應(yīng)的方法
if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
[self.delgate keyboardTool:self didClickItemType:KeyboardItemTypeDone];
}
}
@end
HMKeyboardTool.xib圖:

// HMViewController.h
#import <UIKit/UIKit.h>
@interface HMViewController : UIViewController
@end
// HMViewController.m
#import "HMViewController.h"
#import "HMKeyboardTool.h"
@interface HMViewController ()<HMKeyboardToolDelegate>{
NSArray *_fields;//存儲所有的textField
}
@property (weak, nonatomic) IBOutlet UITextField *birthdayField;
@property (weak, nonatomic) IBOutlet UIView *inputContainer;//輸入框容器view
@end
@implementation HMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1.初始化自定議鍵盤
[self setupCustomKeyboard];
//2.設(shè)置每一個textfield的鍵盤工具view(inputAccessoryView)
[self setupKeyboardTool];
//3.監(jiān)聽鍵盤的事件
[self setupKeyoardNotification];
}
//1.初始化自定議鍵盤
-(void)setupCustomKeyboard{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
datePicker.datePickerMode = UIDatePickerModeDate;
self.birthdayField.inputView = datePicker;
}
//設(shè)置每一個textfield的鍵盤工具view(inputAccessoryView)
-(void)setupKeyboardTool{
//創(chuàng)建工具欄
HMKeyboardTool *tool = [HMKeyboardTool keyboardTool];
//設(shè)置代理
tool.delgate = self;
//1.獲取輸入框窗口所有子控件
NSArray *views = self.inputContainer.subviews;
//創(chuàng)建一個數(shù)據(jù)存儲textfield
NSMutableArray *fieldsM = [NSMutableArray array];
//2.遍歷
for (UIView *child in views) {
//如果子控制是UITextField的時候,設(shè)置inputAccessoryView
if ([child isKindOfClass:[UITextField class]]) {
UITextField *tf = (UITextField *)child;
tf.inputAccessoryView = tool;
//把textfield添加到數(shù)組
[fieldsM addObject:tf];
}
}
_fields = fieldsM;
}
-(void)dealloc{
// 刪除在控制器上的通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//3.監(jiān)聽鍵盤的事件
-(void)setupKeyoardNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
//鍵盤的frame的變化
-(void)kbFrameChange:(NSNotification *)nofifi{
//NSLog(@"%@",nofifi);
//獲取鍵盤改變化的y值
//這鍵盤結(jié)束時的frm
CGRect kbEndFrm =[nofifi.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//鍵盤結(jié)束時的y
CGFloat kbEndY = kbEndFrm.origin.y;
//獲取當(dāng)前的響應(yīng)者
int currentIndex = [self getCurrentResponderIndex];
UITextField *currentTf = _fields[currentIndex];
CGFloat tfMaxY = CGRectGetMaxY(currentTf.frame) + self.inputContainer.frame.origin.y;
//如果textfield的最大值在于鍵盤的y坐,才要往上移
if (tfMaxY > kbEndY) {
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, kbEndY - tfMaxY);
}];
}else{
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
}
#pragma mark 鍵盤工具條的代理
-(void)keyboardTool:(HMKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType{
//獲取當(dāng)前的響應(yīng)者的索引
int currentIndex = [self getCurrentResponderIndex];
NSLog(@"當(dāng)前的響應(yīng)者 %d",currentIndex);
if (itemType == KeyboardItemTypePrevious) {
// 讓上一個field成功響應(yīng)者
[self showProviousField:currentIndex];
}else if(itemType == KeyboardItemTypeNext){
// 讓下一個field成功響應(yīng)者
[self showNextField:currentIndex];
}else{
[self touchesBegan:nil withEvent:nil];
}
}
// 獲取當(dāng)前textfield的響應(yīng)者索引
// 如果返回-1代理沒有找響應(yīng)者
-(int)getCurrentResponderIndex{
//遍歷所有的textfield獲取響應(yīng)者
for (UITextField *tf in _fields) {
if (tf.isFirstResponder) {
return [_fields indexOfObject:tf];
}
}
return -1;
}
//讓上一個field成功響應(yīng)者
-(void)showProviousField:(int)currentIndex{
int proviousIndex = currentIndex - 1;
if (proviousIndex > 0 ) {
UITextField *proviousTf = [_fields objectAtIndex:proviousIndex];
[proviousTf becomeFirstResponder];
}
}
//讓下一個field成功響應(yīng)者
-(void)showNextField:(int)currentIndex{
int nextIndex = currentIndex + 1;
//下一個索引不能超過_fields數(shù)組的個數(shù)
if (nextIndex < _fields.count) {
//讓當(dāng)前響應(yīng)者分發(fā)去
UITextField *currentTf = [_fields objectAtIndex:currentIndex];
[currentTf resignFirstResponder];
UITextField *nextTf = [_fields objectAtIndex:nextIndex];
[nextTf becomeFirstResponder];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
@end
效果圖片:
