iOS,鍵盤附件

項目需求是這樣的:測試

要搞一個鍵盤的附件, 查了些資料, 效果如圖.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成爲第一響應者.
相關文章
相關標籤/搜索