如何抑制虚拟键盘滑入动画?
我在创建模拟天气应用程序行为的模态搜索视图时遇到问题。具体来说,有两个动画困扰着我,并引入了不必要的 0.2 秒延迟:
当模态视图变得可见时,我通过调用
中的 comeFirstResponder 将焦点集中到
。但是,当模态视图滑入时,键盘不可见,但在模态视图转换动画完成后还需要 0.2 秒才能滑入。将调用移至另一个回调(例如UISearchDisplayController.searchBar
上。 viewDidAppearviewWillAppear
或viewDidLoad
)并没有什么好处,键盘不会首先显示。当用户触摸“取消”时,在调用委托的
searchDisplayControllerDidEndSearch
方法之前,会发生另一个动画,展开搜索文本字段并“融化”按钮。同样,这个动画是不需要的,因为模式视图应该在触摸按钮时转换出来。此外,当我关闭并重新呈现相同的视图时,不仅键盘会在转换后滑入,而且取消按钮也会执行相同的操作(幸运的是同时)。
我知道这里描述的类似问题: 键盘动画在模态视图控制器中调用BecomeFirstResponder 时出现的问题。 然而,搜索栏的行为似乎与文本字段的行为略有不同。我无法重现该作者描述的通过在 viewDidLoad
中调用 becomeFirstResponder
使键盘可见的步骤。
问候, 克里斯
I've got a problem with creating a modal search view that emulates the behaviour of that of the Weather app. Specifically, there are two animations, that are bothering me and introduce unneeded 0.2 s delays:
When the modal view becomes visible, I give focus to the
UISearchDisplayController.searchBar
by caling becomeFirstResponder inviewDidAppear
. However, the keyboard is not visible, when the modal view has slid in, but needs another 0.2s to slide in after the animation of tehe modal view transition is complete. Moving the call to another callback likeviewWillAppear
orviewDidLoad
did no good, the keyboard won't show up in the first place.When the user touches cancel, there is another animation taking place, before the delegate's
searchDisplayControllerDidEndSearch
method is called, expanding the search text field and "melting" away the button. Again, this animation is unneded as the modal view is supposed to transition out when the button is touched.Additionally, when I dismiss and re-present the same view, not only does the keyboard slide in after the transition, but the cancel button does the same (luckily simultaneously).
I am aware of a similar problem described here: Keyboard Animation Issues When Calling becomeFirstResponder within a Modal View Controller.
However, it seems like the behaviour of the search bar is sligtly differet then that of text field. I could not reproduce the steps described by that author to make the keyboard visible by calling becomeFirstResponder
in viewDidLoad
.
Regards,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我找到了你的答案。使用界面生成器添加搜索栏时,可以通过两种方式完成:“搜索栏”和“搜索栏和搜索显示控制器”。
我正在使用第二个并且遇到了与您描述的相同的问题。我只能在“viewDidAppear”上调用键盘(使用becomeFirstResponder)。但如果你这样做,只添加搜索栏,它就可以工作。现在我可以在“viewDidLoad”上调用BecomeFirstResponder,并且键盘与视图本身一起出现。
我的意思是多做一点工作,但实际上不多。您必须将控制器设置为搜索栏的委托。我为结果添加了一个列表视图,并使我的控制器成为其委托及其数据源。
I think I found your answer. When you add a search bar using the interface builder, you can do it two ways: "Search bar" and "Search bar and Search Display Controller".
I was using the second and was having the very same problem you described. I could only invoke the keyboard (using becomeFirstResponder) on "viewDidAppear". But if you do it adding just the search bar it works. Now I can call becomeFirstResponder on "viewDidLoad" and the keyboard appears together with the view itself.
I means a little more work, but really not much. You have to set your controller to be the delegate of the search bar. I added a list view for the results and made my controller become its delegate and its datasource.