在我的应用程序中,我正在使用默认 IOS 搜索栏的搜索功能,如果我放置一些字符串进行搜索,但在第一次搜索后我需要显示整个数据源(原始内容)我的功能是搜索字符串是否为空它将显示整个数据源。我的问题是,如果我在默认搜索栏中将搜索字符串设为空,搜索按钮会自动进入隐藏状态。即使字符串为空,我也需要启用搜索按钮。
12 回答
实际上你可以searchBar.enablesReturnKeyAutomatically = NO;
在 iOS 7+ 上设置 Tested
如果您有空字符串,此代码将显示搜索按钮。
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[self.searchBar setShowsCancelButton:YES animated:YES];
self.tblView.allowsSelection = NO;
self.tblView.scrollEnabled = NO;
UITextField *searchBarTextField = nil;
for (UIView *subview in self.searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}
斯威夫特 3/iOS 10
for view1 in searchBar.subviews {
for view2 in view1.subviews {
if let searchBarTextField = view2 as? UITextField {
searchBarTextField.enablesReturnKeyAutomatically = false
break
}
}
}
使用以下代码启用没有文本的返回键
UITextField *searchField = nil;
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField) {
searchField.enablesReturnKeyAutomatically = NO;
}
也许是苹果方面的错误?由于从 .xib 文件设置自动启用返回键无法按预期工作。
只需添加以下代码:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
searchBar.enablesReturnKeyAutomatically = NO;
return YES;
}
创建一个自定义view
& 上view
添加一个button
以删除键盘。当. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
delegate
_ UISearchBar
在该按钮上单击退出键盘以及view
您为此创建的键盘button
。此外,如果您想搜索该按钮单击,那么您也可以这样做。请参阅图片以获取更多说明。
它与接受的答案几乎相同,但如果您使用的是 iOS 7,由于搜索栏的一些变化,您将需要额外的 for 循环
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[self.searchBar setShowsCancelButton:YES animated:YES];
UITextField *searchBarTextField = nil;
for (UIView *mainview in self.searchBar.subviews)
{
for (UIView *subview in mainview.subviews) {
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}
使用它适用于 ios 6 和 7:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)search
{
UITextField *searchBarTextField = nil;
for (UIView *mainview in search.subviews) {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
if ([mainview isKindOfClass:[UITextField class]]) {
searchBarTextField = (UITextField *)mainview;
break;
}
}
for (UIView *subview in mainview.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
searchBarTextField = (UITextField *)subview;
break;
}
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}
如果您正在寻找更优雅的解决方案,您可以使用递归在搜索栏中查找文本字段,如下所示。这应该适用于 ios 6、7 或任何其他未来的 ios,除非苹果有任何弃用。
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
UITextField* textField = [self findTextFieldInView:searchBar];
if (textField) {
textField.enablesReturnKeyAutomatically = NO;
}
}
-(UITextField*)findTextFieldInView:(UIView*)view {
if ([view isKindOfClass:[UITextField class]]) {
return (UITextField*)view;
}
for (UIView* subview in view.subviews) {
UITextField* textField = [self findTextFieldInView:subview];
if (textField) {
return textField;
}
}
return nil;
}
简单的斯威夫特版本:
if let searchTextField:UITextField = searchBar.subviews[0].subviews[2] as? UITextField {
searchTextField.enablesReturnKeyAutomatically = false
}
这是使用 Swift 的解决方案。只需将其粘贴到您的 viewDidLoad 函数中,并确保您在代码中有一个 searchBar 的 IBOutlet。(在我下面的示例中,inputSearchBar 变量是 IBOutlet)
// making the search button available when the search text is empty
var searchBarTextField : UITextField!
for view1 in inputSearchBar.subviews {
for view2 in view1.subviews {
if view2.isKindOfClass(UITextField) {
searchBarTextField = view2 as UITextField
searchBarTextField.enablesReturnKeyAutomatically = false
break
}
}
}