美文网首页ios开发小技巧iOS开发知识小集
自定义键盘工具栏——KeyboardToolBar

自定义键盘工具栏——KeyboardToolBar

作者: Super_Yi | 来源:发表于2016-11-29 15:55 被阅读192次

    今天项目中需要给键盘加上一个工具栏,功能为实现上一步,下一步的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

    相关文章

      网友评论

        本文标题:自定义键盘工具栏——KeyboardToolBar

        本文链接:https://www.haomeiwen.com/subject/gxaapttx.html