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];
}
Related
I use a UISearchDisplayController and its SearchBar like the picture :
As you can see, the view which need to be over the search bar is not correctly placed because of the view before the search bar that need to be here, but I don't know how can I move it.
I have already this code :
#implementation MySearchDisplayController
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
[super setActive: visible animated: NO];
[self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
[self.searchResultsTableView setFrame:CGRectMake(0, 32, 320, 476)];
[self.searchContentsController.view setBackgroundColor:[UIColor whiteColor]];
[self.searchBar setBarTintColor:[UIColor whiteColor]];
}
#end
How can I fix it ?
I specify that if I delete the blue view behind there is no problem so it's the cause of the problem, but I need to put the view at this place.
I have a UISearchBar with some customizations and I create a UISearchDisplayController like this
self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
I want the search bar to appear on navigation bar so I also set
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
Now the search bar shows in the Navigation Bar, but I want to show the search bar of my UISearchDisplayController only when I tap on a Navigation Bar Button Item. I want to have a behaviour like:
Hide the search bar initially
Show search bar when a navigation bar button is clicked
Hide the search bar when I tap "Cancel" button of
the search bar
I tried to hide/unhide it like:
self.searchDisplayController.searchBar.hidden = YES;
but the code doesn't seem working. I've spent a lot time searching the solution to have the behaviour I want and still no luck. Thanks.
Try this out:
CGRect searchFrame = self.searchDisplayController.searchBar.frame;
searchFrame.size.height = 0;
self.searchDisplayController.searchBar.frame = searchFrame;
self.searchDisplayController.searchBar.hidden = YES;
EDIT: I just tried with below code and it worked. See if this helps you!
- (void)viewDidLoad {
[super viewDidLoad];
[self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:YES animated:YES];
[self performSelector:#selector(test) withObject:nil afterDelay:2.0];
}
- (void)test {
[self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:NO animated:YES];
}
Maybe this is a suitable solution?
To complete task 1, in your TVC lifecycle method viewDidLoad, insert a non-animated scroll that places the search bar beneath the Nav bar...
- (void)viewDidLoad {
[super viewDidLoad];
// Scroll off screen the search bar (44 points)
[[self tableView] setContentOffset:CGPointMake(0, 44)];
// other code for method
}
To complete task 2, in your TVC create a custom action method associated with a UIBarButtonItem, that effectively hides the Nav bar, at the same time revealing the search bar...
- (IBAction)hideNavBar:(UIBarButtonItem *)sender {
[[self navigationController] setNavigationBarHidden:YES animated:YES]
}
To complete task 3, in your TVC use the UISearchDisplayController Delegate method to effectively display the Nav bar and at the same time hide the search bar...
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[[self tableView] setContentOffset:CGPointMake(0, 44)];
// You might also like to...
[self setSearchBarText:nil]; // if you are using a property to hold the search bar text
[self setSearchResults:nil]; // if you are using a property to hold the search results
}
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
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.
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.