开发者

Adding input accessory view to UITextField while keyboard is visible

I would like to add an input accessory view to a UITextField while it is the first responder, i.e. showing the keyboard. Apparently, assigning a UIView to the inputAccessoryView property in this state doesn't display this view. I have to first dismiss the keyboard and re-sele开发者_如何学运维ct the UITextField.

Is there a way to add an input accessory view without dismissing and re-selecting?


You can use reloadInputViews on the textView to do that

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


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;
}


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      
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜