// 工具條
// LZKeyboardTool.h
#import <UIKit/UIKit.h>
typedef enum {
KeyboardItemTypePrevious, // 上一個
KeyboardItemTypeNext, // 下一個
KeyboardItemTypeDone // 完成
} KeyboardItemType;
// 定義一個類型
typedef void (^myBlock)(KeyboardItemType);
@interface LZKeyboardTool : UIView
+ (instancetype)keyboardTool;
@property (nonatomic, copy) myBlock pBlock;
@end
// LZKeyboardTool.m
#import "LZKeyboardTool.h"
@interface LZKeyboardTool()
@end
@implementation LZKeyboardTool
// 上一個
- (IBAction)previous:(id)sender {
if (_pBlock) { // 先判斷
_pBlock(KeyboardItemTypePrevious); // 調(diào)用block
}
}
// 下一個
- (IBAction)next:(id)sender {
if (_pBlock) {
_pBlock(KeyboardItemTypeNext);
}
}
// 完成
- (IBAction)done:(id)sender {
if (_pBlock) {
_pBlock(KeyboardItemTypeDone);
}
}
+ (instancetype)keyboardTool{
return [[[NSBundle mainBundle] loadNibNamed:@"LZKeyboardTool" owner:nil options:nil] lastObject];
}
@end
HMKeyboardTool.xib圖:

// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
#import "LZKeyboardTool.h"
@interface ViewController () //<LZKeyboardToolDelegate>
{
NSArray *_fields; // 存儲所有的textField
}
// 生日框
@property (weak, nonatomic) IBOutlet UITextField *birthdayField;
// 輸入框容器
@property (weak, nonatomic) IBOutlet UIView *inputContainer;
/** LZKeyboard數(shù)據(jù)*/
@property (nonatomic, strong) LZKeyboardTool *tool;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.初始化自定義鍵盤
[self setupCustomKeyboard];
// 創(chuàng)建自定義鍵盤
self.tool = [LZKeyboardTool keyboardTool];
// 2.設置每一個textfield的鍵盤工具view(inputAccessoryView)
[self setupKeyboardTool];
// 3.監(jiān)聽鍵盤的事件
[self setupKeyboardNotification];
// 含義,弱引用,防止循環(huán)引用
__weak typeof(self) weakSelf = self;
// 用block保存一段代碼
self.tool.pBlock = ^ (KeyboardItemType itemType){
// 獲取當前響應者的索引
int currentIndex = [weakSelf getCurrentResponderIndex];
switch (itemType) {
case KeyboardItemTypePrevious:
NSLog(@"上一個");
[weakSelf showPreviousField:currentIndex];
break;
case KeyboardItemTypeNext:
[weakSelf showNextField:currentIndex];
break;
case KeyboardItemTypeDone:
[weakSelf touchesBegan:nil withEvent:nil];
break;
}
};
}
// 獲取當前textField的響應者索引
// 如果返回-1代理沒有找到響應者
- (int)getCurrentResponderIndex
{
// 遍歷所有的textField獲取響應者
for (UITextField *tf in _fields) {
if (tf.isFirstResponder) {
return [_fields indexOfObject:tf];
}
}
return -1;
}
// 1.初始化自定義鍵盤
- (void)setupCustomKeyboard
{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
datePicker.datePickerMode = UIDatePickerModeDate;
self.birthdayField.inputView = datePicker;
}
// 2.設置每一個textfield的鍵盤工具view(inputAccessoryView)
- (void)setupKeyboardTool
{
// 創(chuàng)建工具欄
LZKeyboardTool *tool = self.tool;
// 1.獲取輸入框窗口的所有子控件
NSArray *views = self.inputContainer.subviews;
// 創(chuàng)建一個數(shù)據(jù)存儲textfield
NSMutableArray *fieldsM = [NSMutableArray array];
// 2.遍歷
for (UIView *child in views) {
// 如果子控制器是UITextField的時候,設置inputAccessoryView
if ([child isKindOfClass:[UITextField class]]) {
UITextField *tf = (UITextField *)child; // 類型轉(zhuǎn)換
tf.inputAccessoryView = tool;
[fieldsM addObject:tf];
}
}
_fields = fieldsM;
}
- (void)setupKeyboardNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
-(void)dealloc{
// 刪除在控制器上的通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)kbFrameChange:(NSNotification *)notifi
{
// NSLog(@"%@", notifi);
// NSLog(@"%@", notifi.userInfo[@"UIKeyboardFrameEndUserInfoKey"]);
// 獲取鍵盤改變的y值
// 鍵盤結束時的fm
CGRect kbEndFrm = [notifi.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
// 鍵盤結束時的y
CGFloat kEndY = kbEndFrm.origin.y;
// 獲取當前的響應者
int currentIndex = [self getCurrentResponderIndex];
UITextField *currentTf = _fields[currentIndex];
int inputY = self.inputContainer.frame.origin.y;
CGFloat tfMaxY = CGRectGetMaxY(currentTf.frame) + inputY;
NSLog(@"kEndY = %f, tfMaxY = %f, inputY = %d", kEndY, tfMaxY, inputY);
// 改變控制器view的transform
if (tfMaxY > kEndY) {
self.view.transform = CGAffineTransformMakeTranslation(0, kEndY - tfMaxY);
}else{
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformIdentity; // 恢復到原來位置
}];
}
}
#pragma mark -鍵盤工具條的代理
// 讓上一個field成為響應者
- (void)showPreviousField:(int) currentIndex{
int previousIndex = currentIndex - 1;
if (previousIndex >= 0) {
UITextField *previousTf = [_fields objectAtIndex:previousIndex];
[previousTf becomeFirstResponder];
}
}
// 讓下一個field成為響應者
- (void)showNextField:(int) currentIndex{
int nextIndex = currentIndex + 1;
if (nextIndex < _fields.count) {
UITextField *nextTf = [_fields objectAtIndex:nextIndex];
[nextTf becomeFirstResponder];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformIdentity; // 恢復到原來位置
}];
}
@end
效果圖片:

筆者認為這里block使用需要注意的是:
-
block里面保存了一段代碼,里面用到了控制器的self,那么為了避免循環(huán)引用,需要使用下面一段代碼
// 含義,弱引用,防止循環(huán)引用 __weak typeof(self) weakSelf = self; -
調(diào)用block代碼的時候需要進行判斷
// 上一個 - (IBAction)previous:(id)sender { if (_pBlock) { // 先判斷 _pBlock(KeyboardItemTypePrevious); // 調(diào)用block } } -
block保存一段代碼可以這樣寫:
// 用block保存一段代碼 self.tool.pBlock = ^ (KeyboardItemType itemType){ // 獲取當前響應者的索引 int currentIndex = [weakSelf getCurrentResponderIndex]; switch (itemType) { case KeyboardItemTypePrevious: NSLog(@"上一個"); [weakSelf showPreviousField:currentIndex]; break; case KeyboardItemTypeNext: [weakSelf showNextField:currentIndex]; break; case KeyboardItemTypeDone: [weakSelf touchesBegan:nil withEvent:nil]; break; } };