自定義鍵盤(pán)工具欄——KeyboardToolBar

今天項(xiàng)目中需要給鍵盤(pán)加上一個(gè)工具欄,功能為實(shí)現(xiàn)上一步,下一步的textfield的切換和點(diǎn)擊“完成”收起鍵盤(pán),功能比較簡(jiǎn)單,就不用第三方的了,于是自己寫(xiě)了一個(gè)。

toolBar.png

實(shí)現(xiàn)原理是自定義一個(gè)toolBar,作為textfield的inputAccessoryView,并把當(dāng)前界面需要用到工具欄的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

實(shí)現(xiàn)方法:


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

https://github.com/yiyc2008/KeyboardToolBar

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,355評(píng)論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,703評(píng)論 4 61
  • 前言 我們?cè)谑褂面I盤(pán)的時(shí)候,在打字完畢后想要收起鍵盤(pán)繼續(xù)操作,要么是習(xí)慣性點(diǎn)擊界面空白處收起鍵盤(pán),要么是在鍵盤(pán)上方...
    Cloudox_閱讀 1,335評(píng)論 0 1
  • 《精力管理》一書(shū),主要講述了對(duì)自己精力的合理安排利用管理,讓我頗有感觸的兩點(diǎn)式: 1.突破舒適區(qū),一直是成長(zhǎng)的要點(diǎn)...
    優(yōu)秀ing的我閱讀 344評(píng)論 0 0
  • 純粹——是的,純粹。 人性的純粹,一向是我最渴望明了的東西。 我用一生的時(shí)間去探求,卻始終無(wú)法追尋到答案。 于是,...
    至人閱讀 1,625評(píng)論 0 5

友情鏈接更多精彩內(nèi)容