InputAccessoryView停靠在底部 [英] InputAccessoryView docked at bottom

查看:165
本文介绍了InputAccessoryView停靠在底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现与Apple消息应用程序底部文本输入栏相似的定位行为.

I'm trying to achieve similar positioning behavior as the bottom text input bar in Apple's Messages app.

我尝试了很多方法,上下搜索,有很多类似的问题,但没有一个令人满意.

I have tried many approaches, searched high and low and there are many similar questions but none have been satisfactory.

要指定:

  1. 视图底部有一个UIToolbar
  2. 工具栏用于跟随键盘的出现和消失
  3. 当键盘可见时,工具栏应位于键盘顶部
  4. 隐藏键盘后,工具栏停留在视图底部(停靠")
  1. There is a UIToolbar at the bottom of the view
  2. The toolbar is to follow the keyboard as the keyboard appears and disappears
  3. The toolbar should stay on top of the keyboard when the keyboard is visible
  4. When the keyboard is hidden, the toolbar stays ("docked") at the bottom of the view

建议的解决方案:

此解决方案不满足第二个要求的特殊情况(工具栏用于在键盘出现和消失时跟随键盘):

This solution does not meet a special case of the second requirement (the toolbar is to follow the keyboard as the keyboard appears and disappears):

  • 在iOS 7中,引入了UIScrollViewKeyboardDismissMode.它启用了交互式手势,可以关闭键盘.当用户平移超过键盘的顶部边缘时,键盘框架逐渐跟随.该解决方案没有采取任何措施来适应这种行为,只是使工具栏滞留在其动画位置.
  • In iOS 7, UIScrollViewKeyboardDismissMode was introduced. It enables an interactive gesture for dismissing the keyboard. As the the user pans past the top edge of the keyboard, the keyboard frame gradually follows. This solution does nothing to accomodate this behavior and just leaves the toolbar stranded at it's animated position.

此外,此解决方案还无法满足第三个要求的特殊情况(当键盘可见时,工具栏应位于键盘顶部):

In addition, this solution also fails to fulfil a special case of the third requirement (the toolbar should stay on top of the keyboard when the keyboard is visible):

  • 旋转.此解决方案需要额外的,讨厌的无关代码(如我们将在下一个建议的解决方案中看到的那样),以根据设备旋转来旋转工具栏.

此解决方案的另一个问题:

Another issue with this solution:

  • 键盘高度.通过这种解决方案,工具栏不被认为是键盘高度的一部分,因此必须编写其他代码来支持内容的正确插入.

下一个建议的解决方案:

该解决方案似乎是Apple打算支持这种行为的方式,因为它解决了手动设置工具栏动画的所有缺点.但是此解决方案完全没有第四个要求(隐藏键盘时,工具栏停留在视图底部(停靠")).

This solution seems to be the way Apple intended to support this kind of behavior, as it solves all the shortcomings of manually animating the toolbar. But this solution completely misses the fourth requirement (when the keyboard is hidden, the toolbar stays ("docked") at the bottom of the view).

似乎解决方案是使用UIResponderinputAccessoryView,但是以某种方式使inputAccessoryView不能在视图下方移动.我正在寻找干净的代码来完成此任务.在其他地方有精心制作,几乎是高尚的尝试,但如上所述,它们不符合要求.

It seems the solution is to use UIResponder's inputAccessoryView, but somehow making the inputAccessoryView not move below the view. I'm looking for clean code to accomplish this. There are elaborate, almost noble, attempts elsewhere, but as mentioned, they do not meet the requirements.

在我的特定情况下,我希望使用UINavigationController的工具栏,该工具栏会带来其他问题,因为这不是UINavigationController的预期行为.没关系,我愿意介绍一些hacky修复程序来实现这一目标.

In my particular case, I am looking to use UINavigationController's toolbar which entails additional issues as this is not intended behavior for UINavigationController. No matter, I'm willing to introduce some hacky fixes to accomplish that.

推荐答案

Jason Foreman(@threeve)刚刚向我展示了"the"解决方案.在视图控制器上(是,视图控制器)添加inputAccessoryView:并返回要停靠在底部的视图并使用键盘移动.它只是工作.该视图实际上并不需要位于您的视图层次结构中,它将由视图控制器自动插入.

I was just shown "the" solution by Jason Foreman (@threeve). On your view controller (yes, view controller) add inputAccessoryView: and return the view you want to dock at the bottom and move with the keyboard. It just works. The view doesn't actually need to be in your view hierarchy it will be inserted by the view controller automatically.

edit:还实现canBecomeFirstResponder并返回YES(如Max Seelemann所述). reloadInputViews也可以很方便.

edit: also implement canBecomeFirstResponder and return YES (as noted by Max Seelemann). reloadInputViews can be handy too.

这篇关于InputAccessoryView停靠在底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆