Adding input accessory view to UITextField while keyboard is visible

戏子无情 提交于 2019-12-04 06:55:13

If possible only assign the inputAccessoryView once. If you need it to be customized, and can only determine how very late just before becoming the first responder, then I would still only assign it once. But customize the subviews of the inputAccessoryView in the UITextFieldDelegate method textFieldShouldBeginEditing:. Like so:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [self setupInputAccessoryViewForTextField:textField];
    return YES;
}

You can use reloadInputViews on the textView to do that

(I know this is an old post but might help to others)

I just wanted an input accessory view added/removed dynamically. I ended up simply doing this:

[self.responceTextView resignFirstResponder];
self.responceTextView.inputAccessoryView = keyBoardToolbar;
[self.responceTextView becomeFirstResponder];

EDIT: According to @fabian789 this method doesn't work. YMMV.


You could try calling

[myTextField setNeedsLayout];
[myTextField setNeedsRedraw];

to force it to redraw itself?

Disclaimer : This is just what I would try, I don't know if it will work!

I also had to add/remove inputAccessoryView on text change so I ended up changing the inputAccessoryView.alpha value to make it look removed.

The animation is optional but it makes the transition look better:

Swift 4

UIView.animate(withDuration: 0.3) {
  myTextFieldOrTextView.inputAccessoryView?.alpha = 0      
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!