局部禁用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

相关文章
相关标签/搜索