ios 隐藏 UISearchBar 取消按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8496310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide UISearchBar Cancel Button
提问by ArtSabintsev
I have a UISearchDisplayController and UISearchBar hooked up to my ViewController via Outlets from my nib.
我有一个 UISearchDisplayController 和 UISearchBar 通过我笔尖的 Outlets 连接到我的 ViewController。
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?
有没有更好的方法来隐藏它?
回答by Nim Gat
I managed to hide the "Cancel" button by subclassing UISearchBar
and override this method:
我设法通过子类化UISearchBar
并覆盖此方法来隐藏“取消”按钮:
-(void)layoutSubviews{
[super layoutSubviews];
[self setShowsCancelButton:NO animated:NO];
}
回答by BFeher
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 on UIKeyboardWillShowNotification
, and setting [self setShowsCancelButton:NO animated:NO]
there.
对于那些不能或不想子类化的人UISearchDisplayController
,我通过在 上添加一个监听器UIKeyboardWillShowNotification
并[self setShowsCancelButton:NO animated:NO]
在那里设置来解决这个问题。
In viewWillAppear:
:
在viewWillAppear:
:
// Add keyboard observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification
object:nil];
Then you create:
然后你创建:
- (void)keyboardWillAppear:(NSNotification *)notification
{
[YOUR-SEARCHBAR-HERE setShowsCancelButton:NO animated:NO];
}
Don't forget to add,
别忘了补充,
[[NSNotificationCenter defaultCenter] removeObserver:self];
in viewWillDisappear:
!
在viewWillDisappear:
!
Hope this helps!
希望这可以帮助!
回答by quantumkid
Similar to Nimrod's answer, you can also subclass UISearchDisplayController
and implement the setActive:animated:
method:
与 Nimrod 的回答类似,您也可以子类化UISearchDisplayController
并实现该setActive:animated:
方法:
- (void)setActive:(BOOL)visible animated:(BOOL)animated {
[super setActive:visible animated:animated];
self.searchBar.showsCancelButton = NO;
}
回答by ArtSabintsev
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.
这似乎是 Xcode 中的一个错误。我将此错误提交给 Apple 的错误报告站点,他们已跟进要求提供更多示例代码和用例。
Thanks everyone for your attempt at solving this problem.
感谢大家尝试解决这个问题。
回答by William Denniss
Had this problem when using the UISearchBar
with UISearchController
. I'm using my own cancel button, as the cancel button wasn't showing on iPadwith showsCancelButton = YES
, now it won't hide on iPhone with showsCancelButton = NO
!
使用UISearchBar
with时遇到了这个问题UISearchController
。我正在使用我自己的取消按钮,因为取消按钮没有在 iPad上显示showsCancelButton = YES
,现在它不会在 iPhone 上隐藏showsCancelButton = NO
!
The following worked for me.
以下对我有用。
Set the delegate, and initial value:
设置委托和初始值:
- (void)viewDidLoad
{
// ...
self.searchController.searchBar.showsCancelButton = NO;
self.searchController.searchBar.delegate = self;
}
Reset showsCancelButton
to NO
0.1s after the text bar begins editing.
文本栏开始编辑后重置showsCancelButton
为NO
0.1 秒。
#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
self.searchController.searchBar.showsCancelButton = NO;
});
}
回答by Harris
class CustomSearchBar: UISearchBar {
override func setShowsCancelButton(showsCancelButton: Bool, animated: Bool) {
super.setShowsCancelButton(false, animated: false)
}
}
class CustomSearchController: UISearchController, UISearchBarDelegate {
lazy var _searchBar: CustomSearchBar = {
[unowned self] in
let customSearchBar = CustomSearchBar(frame: CGRectZero)
customSearchBar.delegate = self
return customSearchBar
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
}
回答by Jakub Truhlá?
If you want to avoid the subclassing, implement
如果你想避免子类化,实现
searchController.searchBar.showsCancelButton = false;
in these two delegate methods (Do not forget to assign delegates):
在这两个委托方法中(不要忘记分配委托):
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
- (void)didPresentSearchController:(UISearchController *)searchController
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.
每次更新搜索栏时都会调用第一个(默认情况下取消按钮可见),第二个用于第一次激活搜索栏。
回答by Don Miguel
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:
如果在编辑搜索栏的搜索字段时出现取消按钮,您可以执行以下操作;子类化搜索栏并让它实现UITextFieldDelegate
协议:
@interface CustomAlignedSearchBar : UISearchBar<UITextFieldDelegate>
Then implement textFieldDidBeginEditing:
and do something like:
然后实施textFieldDidBeginEditing:
并执行以下操作:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[self setShowsCancelButton:self.cancelButtonShown animated:NO];
}
This will make sure that the cancel button will not show up.
这将确保取消按钮不会出现。
回答by jamesC
Just based on issues I've had before have you tried setting it in:
仅基于我之前遇到的问题,您是否尝试将其设置为:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
I don't know how to ask this question in your question sorry if this is out of place.
我不知道如何在你的问题中问这个问题,如果这是不合适的,抱歉。
回答by ihsan_husnul
After UISearchDisplayController deprecated in iOS8, Apple give handle search presentation to UISearchControllerDelegate.
在 iOS8 中弃用 UISearchDisplayController 后,Apple 将句柄搜索呈现给 UISearchControllerDelegate。
so you can override searchBar to hide the Cancel button, like below :
因此您可以覆盖 searchBar 以隐藏取消按钮,如下所示:
- (void)didPresentSearchController:(UISearchController *)searchController {
[searchController.searchBar setShowsCancelButton:NO];
}
if you need hidden Cancel button from inactive state, you need set searchBar on init :
如果您需要从非活动状态隐藏取消按钮,您需要在 init 上设置 searchBar :
search = [[UISearchController alloc] initWithSearchResultsController:nil];
[search.searchBar setShowsCancelButton:NO];