项目需求是这样的:测试
要搞一个键盘的附件, 查了些资料, 效果如图.atom
首先, UIResponder 中有两个相关的属性, 其实只用到了inputAccessoryView
属性, 在UIResponder中, 这个属性是只读的, 咱们须要在自定义的控制器或者view上, 重写这个属性, @property (nullable, nonatomic, readwrite, strong) UIView *inputAccessoryView;
; 而后自定义一个view, 赋值给 inputAccessoryView
属性便可. 这时候, 在定义readwrite的inputAccessoryView属性的UIResponder子类的, 子视图上面, 弹出键盘时候, 键盘会有一个附件. 你能够彻底自定义这个附件.code
// Called and presented when object becomes first responder. Goes up the responder chain. @property (nullable, nonatomic, readonly, strong) __kindof UIView *inputView NS_AVAILABLE_IOS(3_2); @property (nullable, nonatomic, readonly, strong) __kindof UIView *inputAccessoryView
这是个人测试代码:input
- (void)show { [super show]; [[IQKeyboardManager sharedManager] setEnable:NO]; ESPasswdInputView *passwdInputView = [[ESPasswdInputView alloc] initWithFrame:CGRectZero]; self.inputAccessoryView = passwdInputView; passwdInputView.delegate = self; _passwdInputView = passwdInputView; UITextField *textField = [[UITextField alloc] init]; [textField becomeFirstResponder]; [self addSubview:textField]; [passwdInputView show]; } - (void)dismiss { [[IQKeyboardManager sharedManager] setEnable:YES]; [super dismiss]; [self.passwdInputView dismiss]; self.passwdInputView = nil; }
1.首先若是项目中使用了`IQKeyboardManager`, 则最好关闭掉; 2.自定义的inputAccessoryView不能添加到其它图层之上, 不然崩; 3.测试代码中,使用添加到视图上的一个傀儡textField, 目的是换出键盘, 而后让键盘的附件上的textField成为第一响应者.