局部禁用IQKeyboardManager的toolBar

最近恰好封裝了一個數字鍵盤,可是項目中有用到一個第三方鍵盤管理類IQKeyboardManager,使用該框架以後默認狀況下鍵盤彈起的同時上方會有一個toolbar,入下圖所示。bash

鍵盤.png

個人自定義鍵盤上已經有一個完成按鈕了,這時候就不須要IQKeyboardManager給我加上的toolbar了,該框架裏面有禁用toolBar的方法,全局禁用,個人需求是隻有數字鍵盤的時候纔不須要toolbar,其它的文本輸入框鍵盤彈起時仍是須要toolbar的。app

// 全局禁用,無法達到我要的效果
[IQKeyboardManager sharedManager].enableAutoToolbar = NO;
複製代碼

後來在看IQKeyboardManager.m的源代碼時發現有如下一段代碼:框架

-(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的時候纔會去建立toolbar,發現問題就好辦了。這時候咱們只須要在被響應鍵盤的textfield中加上 self.inputAccessoryView = [UIView new];ui

@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了。spa

相關文章
相關標籤/搜索