I have a UISearchBar in iOS 6 app and it works perfectly, but in iOS 7 the cancel button and the clear button don't work and I can't return back. It's a big problem in my app and I need to solve it.
My code is:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
// -- iOS 7 Hack
if (!SYSTEM_VERSION_LESS_THAN(#"7.0")) {
controller.searchResultsTableView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64);
[controller.searchContentsController.view setNeedsLayout];
}
[self filterContentForSearchText:searchString scope:nil];
return YES;
}
- (void) searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
// iOS7 Hack
if (!SYSTEM_VERSION_LESS_THAN(#"7.0")) {
controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f);
}
}
Thank you for advance.
Possbile duplicate: UISearchBar's Cancel and Clear Buttons Not Working in iOS 7
EDIT:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
self.searchDisplayController.searchBar.hidden = YES;
self.tempImageView.hidden = NO;
[searchBar resignFirstResponder];
}
SOLUTION:
With this function I resolved the problem:
-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
controller.active = YES;
[self.view addSubview:controller.searchBar];
[self.view bringSubviewToFront:controller.searchBar];
}
Hope it helps!
IOS 7 default search bar will be transparent cancel button
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
[aSearchBar resignFirstResponder];
isSearching = NO;
aSearchBar.text = #"";
[diaryTableView reloadData];
}
With this function I resolved the problem:
-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
controller.active = YES;
[self.view addSubview:controller.searchBar];
[self.view bringSubviewToFront:controller.searchBar];
}
Hope it helps!
Related
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)])
{
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)])
{
[self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
//Am using this code but it not working in iOS 8.3 .Is there any problem in storyboard or it is not supported in iOS 8. Am irritating please give me a solution.
Create custom UIBarButtonItem and then set it in your navigation controller
self.navigationItem.leftBarButtonItem = customBarItem;
it might be helpful for you.
Add this 2 lines in the view controller's viewDidLoad method where you want to disable pop gesture
[self.navigationController.interactivePopGestureRecognizer setEnabled:NO];
self.navigationController.interactivePopGestureRecognizer.delegate = self;
In the options the paste and define was OK , but when i click the copy or cut it will crash. I tried to deleted all code except about the search bar , but it did not work on too.and I tried to override the textfield method but the search bar subviews did not contain a UITextField
this is my code
_searchBar = [[UISearchBar alloc] initWithFrame:self.topBar.bounds];
[_searchBar setPlaceholder:LS(#"Search", #"")];
[_searchBar setDelegate:self];
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchController.delegate = self;
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self;
if( _scopeButtonTitles ) {
_searchBar.scopeButtonTitles = _scopeButtonTitles;
_searchBar.showsScopeBar = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_searchBar resignFirstResponder];
}
#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
_searchResultsAvailable = NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
self.loadingTitle = [NSString stringWithFormat:#"%# %#%#",LS(#"Searching", #""),self.title?self.title:#"",#"..."];
_searchResultsAvailable = YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
_searchResultsAvailable = NO;
}
I had debuted the bug many times, and found the error in my app.
It just need to rewrite the copy and cut method for the searchBar.
After I rewrite the methods the bug had gone
I have been attempting to hide a UISearchBar keyboard but my code fails to run successfully. I have the following code which removes the cursor but the keyboard continues to show:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder]; // using method search bar
[theSearchBar resignFirstResponder]; // using actual object name
[self.view endEditing:YES];
}
I have set the delegate in the .h as <UISearchBarDelegate> and set searchBar.delegate = self; in .m
Any ideas? Thanks.
Try to use following methods for search an resign first responder:
- (UIView *)findFirstResponderBeneathView:(UIView *)view {
for ( UIView *childView in view.subviews ) {
if ( [childView respondsToSelector:#selector(isFirstResponder)] && [childView isFirstResponder] ) return childView;
UIView *result = [self findFirstResponderBeneathView:childView];
if ( result ) return result;
}
return nil;
}
- (void)hideKeyboard {
[[self findFirstResponderBeneathView:self.view] resignFirstResponder];
}
I use the following code to hide searchbar:
- (void)updateContentOffset
{
self.tableView.contentOffset = CGPointMake(0, 44);
}
- (void)viewWillAppear:(BOOL)animated
{
NSLog(#"%s",__PRETTY_FUNCTION__);
[super viewWillAppear:animated];
[self searchBarShouldEndEditing:self.searchDisplayController.searchBar];
[self performSelector:#selector(updateContentOffset) withObject:nil afterDelay:0.0];
self.fetchedResultsController.delegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.fetchedResultsController performFetch:nil];
[self.tableView reloadData];
[self.tableView scrollRectToVisible:CGRectMake(0, 44, 1, 1) animated:YES];
}
The code works perfectly when view is shown for the first time.
Next time it's shown(returning from another controller) it slides a bit down than it should be and I have no idea why it happens.
OK, in my case I played with a delay and found that making it 0.1 makes it work as planned.
[self performSelector:#selector(updateContentOffset) withObject:nil afterDelay:0.1];
I created a search bar programmatically and added to my view using the codes below:
- (void)viewDidLoad
{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(xPositionForSearchBar, yPositionForSearchBar, widthForSearchBar, heightForSearchBar)];
UIView *bg = [[searchBar subviews] objectAtIndex:0];
searchBar.delegate = self;
searchBar.placeholder = #"Search record";
for(UIView *view in searchBar.subviews){
if([view isKindOfClass:[UITextField class]]){
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
[bg removeFromSuperview];
[self.view addSubview: searchBar];
}
The code is implemented with UISearchBarDelegate and UITextFieldDelegate.
I have tried using
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
NSLog(#"cancel clicked");
searchBar.text = #"";
[aSearchBar resignFirstResponder];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
NSLog(#"clear");
[self performSelector:#selector(searchBarCancelButtonClicked:) withObject:searchBar afterDelay: 0.1];
return YES;
}
and yet, the text inside the searchBar is not cleared at all when i click on the "clear button" - a circle with a "X" inside.
The clear button works when I implemented it in IB. Wonder why?
Kindly advice, many thanks.
This might happen if you position your search bar out of the bounds of a parent view. Then, the touches aren't delivered to your searchBar properly. This also affects text editing controls like copy & paste.
I have checked your code it work fine on device as well as on simulator.