当键盘可见时将输入附件视图添加到 UITextField

发布于 2024-11-06 09:35:51 字数 216 浏览 1 评论 0原文

我想向 UITextField 添加一个输入附件视图,同时它是第一响应者,即显示键盘。显然,在此状态下将 UIView 分配给 inputAccessoryView 属性不会显示此视图。我必须首先关闭键盘并重新选择 UITextField

有没有一种方法可以添加输入附件视图而无需关闭并重新选择?

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-select the UITextField.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤星 2024-11-13 09:35:52

您可以在 textView 上使用 reloadInputViews 来执行此操作

(我知道这是一篇旧帖子,但可能对其他人有帮助)

You can use reloadInputViews on the textView to do that

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

梦里南柯 2024-11-13 09:35:52

如果可能,仅分配 inputAccessoryView 一次。如果你需要定制它,并且只能确定在成为第一响应者之前多久,那么我仍然只会分配一次。但是在UITextFieldDelegate方法textFieldShouldBeginEditing:中自定义inputAccessoryView的子视图。就像这样:

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

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;
}
寄与心 2024-11-13 09:35:52

我只想动态添加/删除输入附件视图。
我最终只是这样做了:

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

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];
醉殇 2024-11-13 09:35:52

编辑:根据@fabian789,此方法不起作用。 YMMV。


您可以尝试调用

[myTextField setNeedsLayout];
[myTextField setNeedsRedraw];

强制它重新绘制自己吗?

免责声明:这只是我会尝试的,我不知道它是否会起作用!

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!

像极了他 2024-11-13 09:35:52

我还必须在文本更改时添加/删除 inputAccessoryView,因此我最终更改了 inputAccessoryView.alpha 值以使其看起来已删除。

动画是可选的,但它使过渡看起来更好:

Swift 4

UIView.animate(withDuration: 0.3) {
  myTextFieldOrTextView.inputAccessoryView?.alpha = 0      
}

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      
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文