iOS7 searchDisplayController hides tableview indexes - ios

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.

Related

Unwinding to view with search results controller active leaves black gap at top

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.

UISearchBar is disappearing after single tap on search bar

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

displaysSearchBarInNavigationBar does not display search bar

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;

adding UISearchBar to UICollectionviewController embedded into NavigationController IOS6

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];
}

Focus on the UISearchBar but the keyboard not appear

I've read so many solution on how can i focus a searchbar to make keyboard appear when i open my search view, and all of that are like this
[searchBar becomeFirstResponder];
mine is
[self.searchDisplayController.searchBar becomeFirstResponder];
but I tried both.
Now, I tried this, and I also added a
[self.searchDisplayController setActive:YES];
because I'm using a SearchDisplayController, but so far the best result i can have is to have the cursor on the searchbar, the uitableview with an overlay on it, but still no keyboard.
If I run the simulator I can type on the searchbar with my computer's keyboard, but on an iPhone I can't do anything.
If you want to give a look to my code: http://nopaste.info/39fcda4771.html
the focus should be executed in viewDidLoad method
Thanks again.
I was showing searchbar on textFieldDidBeginEditing:(UITextField *)textField delegate method.
Keyboard was not showing. So for that first resign textField as firstResponder.
i.e.
[textField resignFirstResponder];
Then call method with delay
[self performSelector:#selector(callSearchBar) withObject:NULL afterDelay:0.2];
Where
-(void)callSearchBar{
[self.searchDisplayController setActive: YES animated: YES];
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar becomeFirstResponder];
}
It works
Use the UISearchBarDelegate and declare the searchBar in the header file like this ...
#interface mySearchScreen : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> {
UITableView *myTableView;
NSMutableArray *tableData;
UISearchBar *sBar;//search bar
and declare your search bar in loadView
- (void)loadView {
[super loadView];
sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,30)];
sBar.delegate = self;
[self.view addSubview:sBar];
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 31, 300, 400)];
myTableView.delegate = self;
myTableView.dataSource = self;
[self.view addSubview:myTableView];
tableData = [[NSMutableArray alloc]init];
}
and then use becomeFirstResponder on your search bar in viewDidAppear
- (void)viewDidAppear:(BOOL)animated {
//ensure the keyboard comes up from the start
[sBar becomeFirstResponder];
[super viewDidAppear:animated];
}
The sequence matter
[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController.searchBar becomeFirstResponder];
If no luck then check if delegate of the search display controller is linked. Also worth checking the tint colour of the search bar.
On the simulator, make sure that you click on Toggle Software Keyboard to use the keyboard of the simulator as you can see in the image.

Resources