局部禁用IQKeyboardManager的toolBar

最近剛好封裝了一個數(shù)字鍵盤,但是項目中有用到一個第三方鍵盤管理類IQKeyboardManager,使用該框架之后默認(rèn)情況下鍵盤彈起的同時上方會有一個toolbar,入下圖所示。

鍵盤.png

我的自定義鍵盤上已經(jīng)有一個完成按鈕了,這時候就不需要IQKeyboardManager給我加上的toolbar了,該框架里面有禁用toolBar的方法,全局禁用,我的需求是只有數(shù)字鍵盤的時候才不需要toolbar,其它的文本輸入框鍵盤彈起時還是需要toolbar的。

// 全局禁用,沒法達(dá)到我要的效果
[IQKeyboardManager sharedManager].enableAutoToolbar = NO;

后來在看IQKeyboardManager.m的源代碼時發(fā)現(xiàn)有以下一段代碼:

-(void)addToolbarIfRequired
{
    CFTimeInterval startTime = CACurrentMediaTime();
    [self showLog:[NSString stringWithFormat:@"****** %@ started ******",NSStringFromSelector(_cmd)]];
    
    //  Getting all the sibling textFields.
    NSArray *siblings = [self responderViews];
    
    [self showLog:[NSString stringWithFormat:@"Found %lu responder sibling(s)",(unsigned long)siblings.count]];

    //Either there is no inputAccessoryView or if accessoryView is not appropriate for current situation(There is Previous/Next/Done toolbar).
    //setInputAccessoryView: check   (Bug ID: #307)
    if ([_textFieldView respondsToSelector:@selector(setInputAccessoryView:)])
    {
        if ([_textFieldView inputAccessoryView] == nil ||
            [[_textFieldView inputAccessoryView] tag] == kIQPreviousNextButtonToolbarTag ||
            [[_textFieldView inputAccessoryView] tag] == kIQDoneButtonToolbarTag)
        { 
          // ......
    
        }
    }
}

_textFieldView inputAccessoryView] == nil的時候才會去創(chuàng)建toolbar,發(fā)現(xiàn)問題就好辦了。這時候我們只需要在被響應(yīng)鍵盤的textfield中加上 self.inputAccessoryView = [UIView new];

@implementation EBNumberTextField

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        EBNumberKeyboardView *numberKeyboardView = [[EBNumberKeyboardView alloc] initWithKeyboardType:EBNumberKeyboardTypeDecimal];
        numberKeyboardView.delegate = self;
        self.inputView = numberKeyboardView;
        self.inputAccessoryView = [UIView new];
    }
    return self;
}
.....
.....
@end

EBNumberTextField繼承自UITextField,給inputAccessoryView指定一個空的View,這樣在彈出鍵盤的時候,IQKeyboardManager就不會再加上toolbar了。

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

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