隐藏 UISearchBar 取消按钮
我有一个 UISearchDisplayController 和 UISearchBar 通过笔尖上的插座连接到我的 ViewController。
我想隐藏取消按钮,以便用户永远看不到它。问题在于,以下代码隐藏了按钮,但仅在向用户显示一毫秒后才隐藏(例如,它在模拟器和设备上闪烁,然后从视图中消失)。
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
controller.searchBar.showsCancelButton = NO;
}
有没有更好的方法来隐藏它?
I have a UISearchDisplayController and UISearchBar hooked up to my ViewController via Outlets from my nib.
I'd like to hide the cancel button so that the user never sees it. The problem is that the following code hides the button, but only after displaying it to the user for a millisecond (e.g., it flashes on the simulator and device and then disappears out of view).
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
controller.searchBar.showsCancelButton = NO;
}
Is there a better way to hide it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在 iOS 13.0 及更高版本上,UISearchController 具有您可以使用的此属性:
On iOS 13.0 and later, UISearchController has this property you can use:
我设法通过子类化 UISearchBar 并重写此方法来隐藏“取消”按钮:
I managed to hide the "Cancel" button by subclassing
UISearchBar
and override this method:我有同样的问题,但以不同的方式解决了它。
对于那些不能或不想子类化
UISearchDisplayController
的人,我通过在UIKeyboardWillShowNotification
上添加侦听器并设置[self setShowsCancelButton:NO 解决了该问题动画:没有]
那里。在
viewWillAppear:
: 中,然后创建:
不要忘记添加,
在
viewWillDisappear:
中!希望这有帮助!
I had the same issue, but fixed it a different way.
For those who can't or don't want to subclass
UISearchDisplayController
, I fixed the issue by adding a listener onUIKeyboardWillShowNotification
, and setting[self setShowsCancelButton:NO animated:NO]
there.In
viewWillAppear:
:Then you create:
Don't forget to add,
in
viewWillDisappear:
!Hope this helps!
与 Nimrod 的答案类似,您还可以子类化
UISearchDisplayController
并实现setActive:animated:
方法:Similar to Nimrod's answer, you can also subclass
UISearchDisplayController
and implement thesetActive:animated:
method:这似乎是 Xcode 中的一个错误。我向苹果的错误报告网站提交了这个错误,他们随后要求提供更多示例代码和用例。
感谢大家尝试解决这个问题。
This seems to be a bug within Xcode. I submitted this error to Apple's bug reporting site, and they've followed up asking for more sample code and use-cases.
Thanks everyone for your attempt at solving this problem.
将
UISearchBar
与UISearchController
一起使用时遇到此问题。我正在使用自己的取消按钮,因为取消按钮 未在 iPad 上显示且showsCancelButton = YES
,现在它不会在 iPhone 上隐藏showsCancelButton = NO
!以下内容对我有用。
设置委托和初始值:
在文本栏开始编辑后 0.1 秒将
showsCancelButton
重置为NO
。Had this problem when using the
UISearchBar
withUISearchController
. I'm using my own cancel button, as the cancel button wasn't showing on iPad withshowsCancelButton = YES
, now it won't hide on iPhone withshowsCancelButton = NO
!The following worked for me.
Set the delegate, and initial value:
Reset
showsCancelButton
toNO
0.1s after the text bar begins editing.如果你想避免子类化,请
在这两个委托方法中实现 (不要忘记分配委托):
每次更新 searchBar 时都会调用第一个方法(取消按钮默认可见),第二个用于第一次激活 searchBar。
If you want to avoid the subclassing, implement
in these two delegate methods (Do not forget to assign delegates):
The first one is called everytime you update the searchBar (Cancel button is visible by default) and the second one is for the first searchBar activation.
只是基于我之前遇到的问题,您是否尝试将其设置为:
我不知道如何在您的问题中提出这个问题,如果这是不合适的,抱歉。
Just based on issues I've had before have you tried setting it in:
I don't know how to ask this question in your question sorry if this is out of place.
如果在编辑搜索栏的搜索字段时出现取消按钮,您可以执行以下操作;子类化搜索栏并让它实现
UITextFieldDelegate
协议:然后实现
textFieldDidBeginEditing:
并执行以下操作:这将确保取消按钮不会显示。
If the cancel button shows up when editing the search field of the search bar you could do the following; subclass the search bar and have it implement the
UITextFieldDelegate
protocol:Then implement
textFieldDidBeginEditing:
and do something like:This will make sure that the cancel button will not show up.
在 iOS8 中弃用 UISearchDisplayController 之后,Apple 将句柄搜索呈现交给了 UISearchControllerDelegate。
所以你可以重写 searchBar 来隐藏取消按钮,如下所示:
如果你需要在非活动状态下隐藏取消按钮,你需要在 init 上设置 searchBar :
After UISearchDisplayController deprecated in iOS8, Apple give handle search presentation to UISearchControllerDelegate.
so you can override searchBar to hide the Cancel button, like below :
if you need hidden Cancel button from inactive state, you need set searchBar on init :