当键盘可见时将输入附件视图添加到 UITextField
我想向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在 textView 上使用 reloadInputViews 来执行此操作
(我知道这是一篇旧帖子,但可能对其他人有帮助)
You can use reloadInputViews on the textView to do that
(I know this is an old post but might help to others)
如果可能,仅分配
inputAccessoryView
一次。如果你需要定制它,并且只能确定在成为第一响应者之前多久,那么我仍然只会分配一次。但是在UITextFieldDelegate
方法textFieldShouldBeginEditing:
中自定义inputAccessoryView
的子视图。就像这样: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 theinputAccessoryView
in theUITextFieldDelegate
methodtextFieldShouldBeginEditing:
. Like so:我只想动态添加/删除输入附件视图。
我最终只是这样做了:
I just wanted an input accessory view added/removed dynamically.
I ended up simply doing this:
编辑:根据@fabian789,此方法不起作用。 YMMV。
您可以尝试调用
强制它重新绘制自己吗?
免责声明:这只是我会尝试的,我不知道它是否会起作用!
EDIT: According to @fabian789 this method doesn't work. YMMV.
You could try calling
to force it to redraw itself?
Disclaimer : This is just what I would try, I don't know if it will work!
我还必须在文本更改时添加/删除
inputAccessoryView
,因此我最终更改了inputAccessoryView.alpha
值以使其看起来已删除。动画是可选的,但它使过渡看起来更好:
Swift 4
I also had to add/remove
inputAccessoryView
on text change so I ended up changing theinputAccessoryView.alpha
value to make it look removed.The animation is optional but it makes the transition look better:
Swift 4