自定义键盘工具栏——KeyboardToolBar

今天项目中需要给键盘加上一个工具栏,功能为实现上一步,下一步的textfield的切换和点击“完成”收起键盘,功能比较简单,就不用第三方的了,于是自己写了一个。

toolBar.png

实现原理是自定义一个toolBar,作为textfield的inputAccessoryView,并把当前界面需要用到工具栏的textfield传给toolBar。
代码如下:

#import <UIKit/UIKit.h>

@interface KeyboardToolBar : UIToolbar

- (instancetype)initWithArray:(NSArray *)array;

@end

#import "KeyboardToolBar.h"

@interface KeyboardToolBar()

@property (strong, nonatomic) NSArray *array;

@end

@implementation KeyboardToolBar

- (instancetype)initWithArray:(NSArray *)array {
    self = [[KeyboardToolBar alloc]initWithFrame:CGRectMake(0,0, kWidth,35)];
    _array = array;
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kWidth - 60, 5,50, 25)];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitle:@"完成" forState:UIControlStateNormal];
    [button setTitleColor:UICOLOR_MAKE_RGB(26, 141, 248) forState:UIControlStateNormal];
    button.layer.borderColor = UICOLOR_MAKE_RGB(26, 141, 248).CGColor;
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 3;
    [button addTarget:self action:@selector(finishBtnClick)forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    if (array.count >= 2) {
        UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 5, 50, 25)];
        backBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [backBtn setTitle:@"上一步" forState:UIControlStateNormal];
        [backBtn setTitleColor:UICOLOR_MAKE_RGB(26, 141, 248) forState:UIControlStateNormal];
        [self addSubview:backBtn];
        [backBtn addTarget:self action:@selector(backBtnClick)forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(kWidth - 130, 5, 50, 25)];
        nextBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [nextBtn setTitleColor:UICOLOR_MAKE_RGB(26, 141, 248) forState:UIControlStateNormal];
        [self addSubview:nextBtn];
        [nextBtn addTarget:self action:@selector(nextBtnClick)forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)finishBtnClick {
    [_array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UITextField *tf = _array[idx];
        [tf resignFirstResponder];
    }];
}

- (void)backBtnClick {
    [_array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UITextField *tf = _array[idx];
        if ([tf isFirstResponder] && tf != _array.firstObject) {
            UITextField *nextTf = _array[idx - 1];
            [nextTf becomeFirstResponder];
            *stop = YES;
        } else {
            [tf resignFirstResponder];
        }
    }];
}

- (void)nextBtnClick {
    [_array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UITextField *tf = _array[idx];
        if ([tf isFirstResponder] && tf != _array.lastObject) {
            UITextField *nextTf = _array[idx + 1];
            [nextTf becomeFirstResponder];
            *stop = YES;
        } else {
            [tf resignFirstResponder];
        }
    }];
}

@end

实现方法:


KeyboardToolBar *bar = [[KeyboardToolBar alloc]initWithArray:@[_phoneTf,_noteTF]];
    self.phoneTf.inputAccessoryView = bar;
    self.noteTF.inputAccessoryView = bar;

https://github.com/yiyc2008/KeyboardToolBar

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,950评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,191评论 4 61
  • 前言 我们在使用键盘的时候,在打字完毕后想要收起键盘继续操作,要么是习惯性点击界面空白处收起键盘,要么是在键盘上方...
    Cloudox_阅读 4,960评论 0 1
  • 《精力管理》一书,主要讲述了对自己精力的合理安排利用管理,让我颇有感触的两点式: 1.突破舒适区,一直是成长的要点...
    优秀ing的我阅读 2,463评论 0 0
  • 纯粹——是的,纯粹。 人性的纯粹,一向是我最渴望明了的东西。 我用一生的时间去探求,却始终无法追寻到答案。 于是,...
    至人阅读 5,397评论 0 5