最近剛好封裝了一個數(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了。