I want to be able to search for a String without any character in it. The problem is, that the default keyboard does not show up the search button, when there is nothing written in the UISearchBar.
Any idea?
Thanks,
Markus
Luckily, I just happened to be working on code that does exactly this. It's a bit of a hack, but you can find the UITextField that is embedded within the UISearchBar, then turn off enablesReturnKeyAutomatically:
UITextField *searchBarTextField = nil;
for (UIView *subview in searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
Daniels answer works perfect till iOS 7.
iOS 7 Update
- (void)searchBarTextDidBeginEditing:(UISearchBar *) bar
{
UITextField *searchBarTextField = nil;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ? bar.subviews : [[bar.subviews objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}
Type * and use that as a placeholder in your program for "" (nothing).
Searching for "" is like searching for nil / NULL: the query will return all entries that are empty or null.
The * is a wildcard character used in most searching markups, and it'll return all matches.
while searching a text. it is difficult to search a string without character...
so you need to work on
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
//write code here
}
it will run automatically
this is what i did
To remove keyboard upon backspace till blank:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if ([searchText isEqualToString:#""]) {
[searchBar resignFirstResponder];
}
}
you will also need to set your uitextfield within your uisearchbar to the same delegate , remember to add to this delegate (in my code's context the delegate is self)
for (UIView *view in searchBar.subviews){
if ([view isKindOfClass: [UITextField class]]) {
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
following that add these to your delegate
- (void)searchBarCancelButtonClicked:(UISearchBar *) aSearchBar {
[aSearchBar resignFirstResponder];
}
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
[self performSelector:#selector(searchBarCancelButtonClicked:) withObject:textField.superview afterDelay: 0.1];
return YES;
}
when any of these trigger , perform your search for "" string
Related
I have a search and i want when it's clicked to dispaly the last table with results , I did this but i get :
How can I remove that grey view ?
I was trying to use this , but no result :
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = YES;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchDisplayController.searchResultsTableView.hidden=NO;
[self.searchDisplayController.searchBar bringSubviewToFront:self.searchDisplayController.searchResultsTableView];
[self.search becomeFirstResponder];
}
Try this :
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
for (UIView *v in [[[controller.searchResultsTableView superview] superview] subviews]) {
if (v.frame.origin.y == 64) {
[v setHidden:YES];
}
}
}
Through some fantastic help from a very patient user I managed to get my reset button working with the following code:
- (IBAction)resetAction:(id)sender {
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
textField.text = #"";
}
}
The issue I am having now is that this will not reset the textfields in a scroll view. Any help will be gratefully received! I am so close to completing this!!!!
Many Thanks
You could call the method recursivly:
- (void)resetTextfieldsInView:(UIView*)view {
for (UIView *v in [view subviews]) {
if ([v isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)v;
textField.text = #"";
}else if ([v isKindOfClass:[UITextView class]]) {
UITextView *textView = (UITextView *)v;
textView.text = #"";
}else{
[self resetTextfieldsInView:v];
}
}
}
- (IBAction)resetAction:(id)sender {
[self resetTextfieldsInView:self.view];
}
If the textfields are embedded in a scroll view, then you need to iterate the subviews of the scroll view, i.e.
for (UIView *view in [self.scrollView subviews])
I am new to ios. I have the following code to change the return key type of keyboard to join. It works fine in ios6 but not in ios7. The code inside the if-block is never executed on ios7.
Have anyone seen a similar issue? Is there any workaround for this?
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
[searchBar resignFirstResponder];
[(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyJoin];
[searchBar becomeFirstResponder];
}
I googled and found that Join button isn't showing up because the internal structure of UISearchBar, which aren't supposed to modify, has changed. I dont konw how to fix this and where to found out what has changed, Anyone can explain ?
Also tried following(similar way) workaround code not lucky enough
for(UIView *searchBarSubview in [searchBar subviews]) {
if([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)searchBarSubview setReturnKeyType: UIReturnKeyJoin];
} else {
for(UIView *subSubView in [searchBarSubview subviews]) {
if([subSubView conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)subSubView setReturnKeyType: UIReturnKeyJoin];
}
}
}
Try this :
UITextField *searchBarTextField ;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ?
self.searchBar.subviews : [[self.searchBar.subviews
objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.returnKeyType = UIReturnKeyJoin;
try this for get textfield from searchbar
for (UIView *subView in self.searchBar.subviews){
for (UIView *searchView in subView.subviews){
if ([searchView isKindOfClass:[UITextField class]])
{
[(UITextField *)searchView setKeyboardAppearance:UIKeyboardAppearanceAlert];
[(UITextField *)searchView setEnablesReturnKeyAutomatically:NO];
break;
}
}
}
In iOS6 , I use the below code to change inputView of UISearchbar
for (UIView *view in _searchBar.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField* textField = (UITextField*)view;
[textField setInputView:_myKeyboard];
break;
}
}
UISearchbar in iOS7 had changed, I don't know how to find textField to change inputView.
Please help! Thanks!
The hierarchy of subview has been changed in iOS7, so you can use the following code:
// subviews
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *view in searchBarSubViews) {
if([view isKindOfClass:[UITextField class]])
{
UITextField* search=(UITextField*)view;
[search setFont:[UIFont fontWithName:#"MyCustomFont" size:15]];
search.delegate = self;
[search setInputView:self.customKeyboard];
[self.customKeyboard setTextView:search];
}
}
[self.searchBar reloadInputViews];
Use following code :
NSArray *subViewsOfSearchBar = [[self.YOurSearchBar.subviews objectAtIndex:0] subviews];
for(int i =0; i< subViewsOfSearchBar.count; i++) {
if([[subViewsOfSearchBar objectAtIndex:i] isKindOfClass:[UITextField class]])
{
UITextField *searchTxtField=(UITextField*)[subViewsOfSearchBar objectAtIndex:i];
[searchTxtField setInputView:self.customKeyboard];
}
}
[self.searchBar reloadInputViews];
Try this code :
for (UIView *view in _searchBar.inputView.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField* textField = (UITextField*)view;
[textField setInputView:_myKeyboard];
break;
}
}
in iOS 7 subview is not working. From iOS7 UI objects have a extra wrapper/container. May be this code will help you.
I am using a UISearchBar to search a list, but when I click on the search button
on the keyboard, the keyboard does not hide.
Please help me to fix this issue.
Here is my code
-(void)search:(UISearchBar*)searchbar Text:(NSString*)text
{
[copyListOfItems removeAllObjects];
if([text length] > 0) {
// [ovController.view removeFromSuperview];
searching = YES;
NSString *searchText = searchBar.text;
for(imergencyData *objtrust in aryTrustee)
{
NSRange titleResultsRange = [objtrust.strName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:objtrust];
}
NSLog(#"number of object found %d",copyListOfItems.count);
}
else
{
// [tblTrustee insertSubview:ovController.view aboveSubview:self.parentViewController.view];
[searchbar resignFirstResponder];
searching = NO;
//tblTrustee.scrollEnabled = NO;
}
[tblTrustee reloadData];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar
{
[aSearchBar resignFirstResponder];
[self.view endEditing:YES];
}
//Method call when type text in search box
-(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
[self search:theSearchBar Text:searchText];
}
//method call when on search button
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBa{
[searchBa endEditing:YES];
[searchBa resignFirstResponder];
[self search:searchBa Text:searchBar.text];
}
put this line of code and try it once,
- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar {
[aSearchBar resignFirstResponder];
[self.view endEditing:YES];
}
(or)
EDIT:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
[self.view endEditing:YES];
}
Set first searchbar.delegate = self then in - (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar call [searchbar resignFirstRespnder]
Good Luck
You can use below delegate which is called when searchbar Search button is clicked.
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
[theSearchBar resignFirstResponder];
}
Or use this as :
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
[self keyBoradRemove];
}
- (void) keyBoradRemove
{
searchBar.text = #"";
[searchBar resignFirstResponder];
}
here searchBar is an instance of UISearchBar.
UISearchBar *searchBar;
Hope it helps you.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
use search bar delegate as
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1
{
[searchBar1 resignFirstResponder];
}
use this code for hide keyboard on click of search button of keyboard
- (void)searchBarTextDidBeginEditing:(UISearchBar *) bar
{
UITextField *searchBarTextField = nil;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ? bar.subviews : [[bar.subviews objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
But before that kindly check whether you have set the search bar delegate properly.
for swift 1.2 you can touch outside or click search , in both cases keyboard will hide
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}