Enable "Search Button" even though search bar textfield is empty
I'm looking for a way to enable the "Search"-Button on the Keyboard even tough the search bar's text field开发者_JAVA技巧 doesn't contain text at that very moment.
background is, that along with the term entered in the search bar's textfield i'll send other information to my backend. A Search for a null-term would then make sense (in my scope)..
is there any way to solve this problem?
thx in advance,
sam
You could try adding an invisible character to the beginning of the search text field when it becomes active. A Zero-width space might be just the thing.
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController*)controller
{
NSLog(@"Starting search");
controller.searchBar.text = @"\u200B";
}
You might want to combine it with something like this to prevent a reload of the table data:
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString
{
return ![searchString isEqualToString:@"\u200B"];
}
It's very simple in iOS 8.
In your viewDidLoad:
self.searchBar.enablesReturnKeyAutomatically = false
But for iOS version prior to 8, you should set UISearchBarDelegate to your searchBar, and then override the searchBarTextDidBeginEditing (mine is Swift code, it's easy to convert it to Objective-C code):
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
var searchBarTextField : UITextField? = nil
for mainview in searchBar.subviews
{
for subview in mainview.subviews {
if subview.isKindOfClass(UITextField)
{
searchBarTextField = subview as? UITextField
break
}
}
}
searchBarTextField!.enablesReturnKeyAutomatically = false
}
It would be a better idea to just have a button or link for "Show All" or some similar thing.
精彩评论