I have searched all other questions about UISearchBar and nothing seems to answer my issue. I have a navigation view at the top and a tableview with a UISearchBar. When I tap on the search bar, the tableview gets darkened, the keyboard comes up, both as they should, but the uisearchbar disappears. If I tap on the darkened out tableview, the keyboard gets dismissed, the darkeness goes away, and the UISearchBar reappears. Any idea why it is disappearing?
Here's some code which is inside a class which extends UITableViewController:
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 44)];
searchBar.delegate = self;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
//[self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.tableView];
self.tableView.tableHeaderView = searchDisplayController.searchBar;
In my .h file, I adopt these protocals: ` but I do not implement any of their methods except:
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
[tableView registerClass:[EmailCell class] forCellReuseIdentifier:mailCellIdentifier];
}
EDITED:
I purposely moved the tableview down further, so that there is whitespace between the top navigation view and the tableview, and found that the searchbar wasn't disappearing, it was moving further up on the screen when tapping the UISearchBar, occupying the whitespace that I added in. Any ideas why?
Add this method
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.tableView];
}
}
For more detail check this link
Related
I have multiple view controllers which all use one class (we'll call it searchClass <NSObject>) I wrote that has a UISearchController for searching an external API. The user can tap the search icon and the search controller becomes active: [_searchClass.searchController setActive:YES]; It uses its own table view controller (not the one in each of the view controllers, because they aren't all table view controllers).
In my case I make the search bar appear in the navigation bar. Search works great, the user can select a search result and tap it for a detail view. The problem is when the user goes back (unwinds) from the detail view to the search results, there is a black gap about 44 pts tall that appears briefly above the table view and below the navigation bar, and then disappears.
Here is my setup. In the view controller:
self.definesPresentationContext = YES;
_searchClass = [SearchClass new];
_searchClass.navController = [self navigationController];
In the search class:
_searchTableViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"searchTableViewController"];
_searchTableViewController.tableView.delegate = self;
_searchTableViewController.tableView.dataSource = self;
_searchTableViewController.tableView.backgroundColor = [UIColor clearColor];
_searchTableViewController.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
_searchTableViewController.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
_searchTableViewController.definesPresentationContext = YES;
_searchController = [[UISearchController alloc] initWithSearchResultsController:_searchTableViewController];
_searchController.delegate = self;
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.searchBar.delegate = self;
_searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
_searchController.searchBar.showsCancelButton = YES;
_searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
There are also the usual delegate methods for UISearchBar and UISearchController, and the code that displays the search bar, which uses an animation to replace the titleView of the navigation bar.
How do I get rid of that gap after unwinding back to the search results?
I found the solution, it's related to the UINavigationBar. For some reason setting the translucent property to NO seems to have caused this issue.
self.navigationBar.translucent = YES;
Setting it to YES (or omitting the line since it is the default) made it work properly. Since I still wanted to have the navigation bar be opaque, I did the following to make the nav bar translucent only during transitions, which seems to be where the issue lies:
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBar.translucent = YES;
}
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.navigationController.navigationBar.translucent = NO;
}
Definitely seems like a genuine iOS bug to me.
I added a UISearchBar to my initial UIViewController and everything works fine. However when I go to add the same UISearchBar, the same way to another ViewController that's pushed ontop of the first one, nothing seems to happen when I click into the searchBar. No delegate method is fired, no keyboard comes up, nothing.
This is how I'm adding it to the navigationBar:
- (void)viewDidLoad {
[super viewDidLoad];
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchResults = [NSMutableArray array];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.placeholder = NSLocalizedString(#"Search, eh?", nil);
// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;
}
Like I said it works in the first UIViewController, am I missing something in the other 2-3 viewControllers I've tried adding this too? I don't see why the searchBar shows up in the navBar but nothing is happening upon clicking into it. I've also set the delegate like so:
#interface ViewController () <UISearchResultsUpdating>
Try setting definesPresentationContext to NO on the first view controller as soon as you present a new one on.
I believe that the presentation walks up the view controller hierarchy looking for the first one that defines a context and not down.
Have a tableview with indexes. They are visible on initial load.
I also have a searchDisplayController
When I use the searchDisplayController and then cancel out of it, suddenly the indexes on the original tableview are hidden.
I never had this problem with iOS 6.
Here is my code that worked with iOS 6:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView2 {
if (self.searchDisplayController.active)
return nil;
else
return self.indices;
}
I've tried this with no luck:
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
[self.tableViewOriginal reloadSectionIndexTitles];
}
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
[self.tableViewOriginal reloadSectionIndexTitles];
}
UPDATE:
To add the tableView I use a storyboard and connect it with an IBOutlet. The searchBar and searchDisplayController are added programmatically in viewDidLoad:
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 290, 44)];
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
I then add the searchbar to a table cell if cellForRowAtIndexPath
[cell addSubview:self.searchBar];
One possible issue with your setup is that you are putting the search bar in a table view cell.
A more standard (and much simpler) approach is to make the search bar the table view's headerView.
self.tableView.headerView = self.searchBar;
Just do this once just after setting up the search display controller.
There is plenty of sample code out there, and I thought I was following it line for line; my code:
(void)viewDidLoad
{
[super viewDidLoad];
// init search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
searchBar.showsCancelButton=YES;
// set up searchDisplayController
UISearchDisplayController *searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
// display search bar in nav bar
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
}
With that code, shouldn't I see the search bar displayed in the navigation bar? All I see in the nav bar is the cancel button. I have declared the protocols <UISearchDisplayDelegate, UISearchBarDelegate> in my header file. What am I missing, or what could be going wrong? Thanks
Note:
Using self.navigationItem.titleView = searchBar, the search bar displays as expected. I wonder what are the advantages of the newer displaysSearchBarInNavigationBar method..
-- Edit by tassilo --
It looks like the search bar is added to the navigation bar, but then disappears.
The displaysSearchBarInNavigationBar property makes it so the search bar slides into the navigation bar when the user taps it, like what happens in the Apple Mail app. You still need to add the search bar to the view somewhere.
Best way to do this is to set the tableHeaderView of a UITableViewController to the search bar.
self.tableView.tableHeaderView = self.searchBar;
This line is your problem.
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
self.searchDisplayController is not the same as searchDisplayController, that you created.
So just replace self.searchDisplayController with searchDisplayController. Also, be sure that you don't have #synchronize searchDisplayController;
I would like to add a searchbar to a UICollectionViewController, that's embedded the following way:
UItabbarController > UINavigationbarController > UICollectionViewController > SearchBar (!)
In this view, the search bar would replace the NavigationBar.
Under the same design, if I test the above with a UITableViewController, the searchbar shows up fine (both programmatically and via the Storyboard)
Problem is I can't get to add the search bar over the UICollectionViewController when I use the StoryBoard framework; it just sits in the middle of the view, and I'm clueless as to how to move it to the top. Plus, it always appears below the UICollectionview, so it's not visible.
So, taking the other route, programmatically:
-(void)viewWillAppear:(BOOL)animated{
self.searchBarTop = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.searchBarTop setPlaceholder:#"Enter your command here"];
self.searchDC = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBarTop contentsController:self];
self.searchBarTop.delegate = self;
[[self navigationController] setNavigationBarHidden:NO animated:animated];
[self.navigationController.navigationBar addSubview:self.searchBarTop];
}
With this, the search bar shows up fine. But unfortunately, when I type in some text, it disappears above the view - presumably because the underlying navBar does so - (don't know why...)
I'm not sure exactly why the searchbar is fine with a UITableViewController, and why it is such a pain for a UICollectionViewController.
That said, anyone has a clue as to why the searchbar/navBar disappear, and how I can fix that ?
Any solution is welcome..
thanks !
-A
Add a Header and put the SearchBar in that (that is what I have done in the past). That being said, I have gotten in the habit of hardly ever using either a UITableViewController (unless I am implementing a StaticCell TableView) or a UICollectionViewController. What I would suggest is to implement a standard UIViewController and just add in your UICollectionView. Size the CollectionView down some and put the SearchBar at the top. This allows you to have a SearchBar that is always displayed (which my users generally like better than having to scroll to the top to change, edit a search)
I use the following code to add a UISearchBar to the UICollectionViewController.
Unfortunately I couldn't make UISearchDisplayController working.
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.collectionView.frame), 44)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.delegate = self;
[self.collectionView addSubview:self.searchBar];
[self.collectionView setContentOffset:CGPointMake(0, 44)];
}
- (void) viewWillAppear:(BOOL)animated{
// to show search bar
[self.collectionView setContentOffset:CGPointMake(0, 0)];
// to hide search bar
[self.collectionView setContentOffset:CGPointMake(0, 44)];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[searchBar setText:#""];
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
}