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;
Related
[![enter image description here][1]][1]I am facing issue with UISearchBar & UISearchDisplayController. When I click on searcher then it messes up with the Navigation bar. I have tried to fix with many codes but did not work. I am creating UIsearch bar and UISearchDisplayController via pragmatically. Please help. Any suggestion will be Great.
// Search Bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,subView.frame.size.width, 52)] ;
searchBar.delegate=self;
//UISearchDisplayController
searchController=[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] ;
[searchController setDelegate:self];
[searchController setSearchResultsDataSource:self];
[searchController setSearchResultsDelegate:self];
[searchController.searchResultsTableView setDelegate:self];
In my view controller I am adding a search bar programmatically and it appears exactly the way i want but when i click on it do nothing i can't write/type anything on the search bar, And there is no error on the log. I added search bar in another view controller same way,it does works but in that particular it doesn't work. I am pushing this view controller from previous view controller so it has a navigation back button on the left (check the image) it works too.
In the view controller there is a TableView and a CollectionView i want to add the search for the table view. I was wondering if there is anything to do with having both of this in the same view controller.
Code to add search bar
- (void)viewDidLoad {
//other stuff on view controller
[self setSearchView];
}
-(void)setSearchView{
//searchController is added as a property on .h file
//and all the delegate are also added
//#property (strong, nonatomic) UISearchController *searchController;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater= self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.navigationController.toolbarHidden=YES;
self.navigationItem.titleView=self.searchController.searchBar;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;
}
I am coming to this view controller from a previous view controller and pushing this view controller when a button is clicked .And i am using nib file
Button action
- (void)ButtonClicked {
MYNextViewController *nextViewController = [[MYNextViewController alloc] initWithNibName:#"MYNibFileName" bundle:nil];
// I also have a tab bar on the bottom which i am hiding for that view controller when pushing
nextViewController .hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:nextViewController animated:YES];
}
Image:
Any help will be greatly appreciated. Hope i made my self clear...
Finally i was able to solve this problem with some change in view hierarchy thanks to #Tj3n , but not sure if this is the correct solution but right now its working ..
I also had to remove this line and add few more codes for other purpose
self.definesPresentationContext = YES; //removed...
also answer of that questions was helpful
I had a similar issue and tried the answers here, but it didnt work for me. Then stumbled upon this post https://forums.developer.apple.com/thread/122972
Basically for me disabling Automatically Sync Pasteboard worked. (Simulator -> Edit -> Automatically Sync Pasteboard)
//Please use this code
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.navigationController.navigationBar.bounds.size.height)];
_searchBar.tintColor = [UIColor blueColor];
//[_searchBar setImage:[UIImage imageNamed:#"cross_icon"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
_searchBar.placeholder = #"Enter drug name";
//_searchBar.showsBookmarkButton = YES;
_searchBar.barTintColor = [UIColor clearColor];
UITextField *txfSearchField = [_searchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor whiteColor];
txfSearchField.tintColor = [UIColor blackColor];
txfSearchField.textColor = [UIColor blackColor];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
_searchBar.showsCancelButton = NO;
[_searchBar becomeFirstResponder];
[self.navigationController.navigationBar addSubview:_searchBar];
For your particular case, Use this.
self.searchController.obscuresBackgroundDuringPresentation = NO;
This will allow you to use the same tableview to interact with search result.
I have two view controllers each one contains UISearchController and a UITableView. The Code in viewDidLoad is:
SearchResultsTableViewController *searchResultController = [[SearchResultsTableViewController alloc]initWithNibName:#"SearchResultsTableViewController" bundle:nil];
UINavigationController *navSearchResultsController = [[UINavigationController alloc] initWithRootViewController:searchResultController];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:navSearchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = YES;
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.tableView.userInteractionEnabled = YES;
The first view controller is the rootViewController, which works perfectly without any problem. The second view controller is being pushed via the navigation controller based on segment selection.
[self.navigationController pushViewController:self.vc];
There is only one problem with the second view controller. When I tap on the searchBar, it animates to the top and goes underneath the navigation bar while the first one animates in its spot and does not go up.
Can anyone tell me how to disable this animation?
I want to keep the searchBar in its same place while searching. It's exactly the same implementation for both view controllers. Why does each one act differently?
Thanks,
Set the hidesNavigationBarDuringPresentation boolean on the UISearchController to NO.
self.searchController.hidesNavigationBarDuringPresentation = NO;
Or try this answer, it sounds similar to what you are trying to do. But you would probably want to adjust this line:
[super setActive: visible animated: animated]; // instead of passing animated, pass NO
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.