UISearchBar 禁用自动禁用取消按钮
我已经在表视图中实现了 UISearchBar,除了一件小事之外几乎所有内容都正常工作:当我输入文本然后按键盘上的搜索按钮时,键盘消失,搜索结果是表中显示的唯一项目,文本保留在 UISearchBar 中,但取消按钮被禁用。
我一直试图让我的列表尽可能接近苹果联系人应用程序的功能,当您在该应用程序中按搜索时,它不会禁用取消按钮。
当我查看 UISearchBar 头文件时,我注意到 _searchBarFlags 结构下有一个 autoDisableCancelButton 标志,但它是私有的。
设置 UISearchBar 时是否缺少某些内容?
I have implemented a UISearchBar into a table view and almost everything is working except one small thing: When I enter text and then press the search button on the keyboard, the keyboard goes away, the search results are the only items shown in the table, the text stays in the UISearchBar, but the cancel button gets disabled.
I have been trying to get my list as close to the functionality of the Apple contacts app and when you press search in that app, it doesn't disable the cancel button.
When I looked in the UISearchBar header file, I noticed a flag for autoDisableCancelButton under the _searchBarFlags struct but it is private.
Is there something that I am missing when I setup the UISearchBar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我找到了解决方案。您可以使用此 for 循环来循环搜索栏的子视图,并在按下键盘上的搜索按钮时启用它。
I found a solution. You can use this for-loop to loop over the subviews of the search bar and enable it when the search button is pressed on the keyboard.
我必须稍微调整一下才能让它在 iOS7 中工作
I had to tweak this a bit to get it work for me in iOS7
有两种方法可以轻松实现此目的,
您应该使用第二种方法,如果 Apple 在后台更改它,它不会使您的应用程序崩溃。
顺便说一句,我从 iOS 4.0 到 8.2 测试了它,没有任何变化,我也在我的商店批准的应用程序中使用它,没有任何问题。
There is two way to achieve this easily
You should go with the second one, it will not crash your application if Apple will change it in the background.
BTW i tested it from iOS 4.0 to 8.2 and no changes, also i use it in my Store approved application without any issues.
这就是让它在 iOS 6 上运行的原因:
This is what made it to work on iOS 6 for me:
这是我的解决方案,适用于所有 iOS 版本的所有情况。
IE 中,其他解决方案无法处理因用户拖动滚动视图而导致键盘消失的情况。
Here's my solution, which works for all situations in all versions of iOS.
IE, other solutions don't handle when the keyboard gets dismissed because the user dragged a scroll view.
根据我的回答,将其放入您的 searchBar 委托中:
As per my answer here, place this in your searchBar delegate:
所有答案都不适合我。我的目标是 iOS 7。但我找到了答案。
我正在尝试类似 Twitter iOS 应用程序。如果您单击“时间轴”选项卡中的放大镜,则会出现
UISearchBar
,同时激活“取消”按钮、显示键盘以及最近的搜索屏幕。滚动最近的搜索屏幕,它会隐藏键盘,但会保持“取消”按钮处于激活状态。这是我的工作代码:
我通过在表视图的
scrollViewWillBeginDragging:
处设置断点来实现此解决方案。我查看了UISearchBar
并显示了它的子视图。它始终只有一个,类型为 UIView(我的变量 searchBarSubview)。然后,
UIView
持有一个名为的NSArray
subviewCache
并且我注意到最后一个元素(即第三个元素)的类型为UINavigationButton
,而不是在公共 API 中。所以我开始改用键值编码。我检查了UINavigationButton
是否响应setEnabled:
,幸运的是,它确实响应了。所以我将该属性设置为@YES
。事实证明,UINavigationButton
是取消按钮。如果苹果决定改变
UISearchBar
内部结构的实现,这肯定会被破坏,但那又怎样呢。目前有效。None of the answers worked for me at all. I'm targeting iOS 7. But I found an answer.
What I'm trying is something like the Twitter iOS app. If you click on the magnifying glass in the Timelines tab, the
UISearchBar
appears with the Cancel button activated, the keyboard showing, and the recent searches screen. Scroll the recent searches screen and it hides the keyboard but it keeps the Cancel button activated.This is my working code:
I arrived at this solution by setting a breakpoint at my table view's
scrollViewWillBeginDragging:
. I looked into myUISearchBar
and bared its subviews. It always has just one, which is of typeUIView
(my variablesearchBarSubview
).Then, that
UIView
holds anNSArray
calledsubviewCache
and I noticed that the last element, which is the third, is of typeUINavigationButton
, not in the public API. So I set out to use key-value coding instead. I checked if theUINavigationButton
responds tosetEnabled:
, and luckily, it does. So I set the property to@YES
. Turns out that thatUINavigationButton
is the Cancel button.This is bound to break if Apple decides to change the implementation of a
UISearchBar
's innards, but what the hell. It works for now.这是一个 Swift 3 解决方案,它利用扩展来轻松获取取消按钮:
现在介绍用法:
Here's a Swift 3 solution that makes use of extensions to get the cancel button easily:
Now for the usage:
对于 Monotouch 或 Xamarin iOS,我有以下适用于 iOS 7 和 iOS 8 的 C# 解决方案:
此答案基于 David Douglas解决方案。
For Monotouch or Xamarin iOS I have the following C# solution working for iOS 7 and iOS 8:
This answer is based on David Douglas solution.
更完整的答案:
searchBarTextDidEndEditing
。
A more complete answer:
searchBarTextDidEndEditing
.
时间过去了,但问题仍然存在......
优雅的 Swift 5/iOS 13 解决方案:
Time passes, but the problem is still there...
Elegant Swift 5/iOS 13 solution: