最近刚好封装了一个数字键盘,但是项目中有用到一个第三方键盘管理类IQKeyboardManager,使用该框架之后默认情况下键盘弹起的同时上方会有一个toolbar,入下图所示。
键盘.png我的自定义键盘上已经有一个完成按钮了,这时候就不需要IQKeyboardManager给我加上的toolbar了,该框架里面有禁用toolBar的方法,全局禁用,我的需求是只有数字键盘的时候才不需要toolbar,其它的文本输入框键盘弹起时还是需要toolbar的。
// 全局禁用,没法达到我要的效果
[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];
@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了。
网友评论