UISearchController methods not being called - ios

I have created a searchBar programatically and added it to the navigation bar tile view. This is my code:
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:self];
searchController.dimsBackgroundDuringPresentation = YES;
// Use the current view controller to update the search results.
searchController.searchResultsUpdater = self;
//Setting Style
searchController.searchBar.barStyle = UIBarStyleBlack;
searchController.searchBar.barTintColor = [UIColor colorWithRed:49.0/255.0 green:49.0/255.0 blue:61.0/255.0 alpha:1.0];
searchController.searchBar.backgroundImage = [UIImage imageNamed:#"BG"];
searchController.searchBar.placeholder = #"Search Artists, Songs, Albums etc.";
searchController.searchBar.keyboardAppearance = UIKeyboardAppearanceDark;
self.definesPresentationContext = YES;
self.navigationItem.titleView = searchController.searchBar;
searchController.delegate = self;
searchController.searchBar.delegate = self;
[searchResultsTableView reloadData];
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
NSLog(#"Halp.jpg");
return YES;
}
This above method along with all other methods aren't being called. My header looks like this:
<UISearchResultsUpdating, UITableViewDelegate, UISearchControllerDelegate, UISearchBarDelegate, UITableViewDataSource>

The problem is probably related to this line:
searchController.searchBar.delegate = self;
I suspect that the UISearchController sets itself as the UISearchBar's delegate, so setting it yourself messes things up in some way.

Related

Why are my UISearchController delegate methods not being called?

I've included the following in my class:
#interface DiscoverNew() <UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate>
I have my UISearchController setup here:
-(void)viewDidLoad {
[self configureSearchController];
}
-(void)configureSearchController{
// Initialize and perform a minimum configuration to the search controller.
UISearchController *searchController = self.searchController;
searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.placeholder = #"Search for friends...";
searchController.delegate = self;
searchController.searchBar.delegate = self;
[searchController.searchBar sizeToFit];
[searchController.searchBar setBarStyle:UIBarStyleBlack];
[searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
searchController.searchBar.tintColor = [GBColor accentGray];
searchController.searchBar.backgroundColor = [GBColor darkGray];
searchController.searchBar.barTintColor = [GBColor accentGray];
searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(18.0f, 0.0f);
// Place searchbar above TableView
self.tableView.tableHeaderView = searchController.searchBar;
}
As you can see, I have called searchController.searchBar.delegate = self; searchController is indeed appearing where it should, and the keyboard is shown when the search bar is tapped, but none of my delegate methods are being called. What's going on?
You have a property named searchController. But then you create a local variable of the same name in your configureSearchController method. So in the end you never set the searchController property. As a result, your locally created search controller goes out of scope, has no more references, and gets deallocated.
The fix is to update your code a little.
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.placeholder = #"Search for friends...";
searchController.delegate = self;
searchController.searchBar.delegate = self;
[searchController.searchBar sizeToFit];
[searchController.searchBar setBarStyle:UIBarStyleBlack];
[searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
searchController.searchBar.tintColor = [GBColor accentGray];
searchController.searchBar.backgroundColor = [GBColor darkGray];
searchController.searchBar.barTintColor = [GBColor accentGray];
searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(18.0f, 0.0f);
self.searchController = searchController;

UISearchBar not firing

I put my UISearchController's search bar on navigation bar's titleView. But when I tap to search, UISearchBar does not respond. What can be the cause ?
let tvController:UITableViewController = UITableViewController()
tvController.tableView.delegate = self
tvController.tableView.dataSource = self
self.searchController = UISearchController(searchResultsController: tvController)
self.searchController?.searchResultsUpdater = self
self.searchController!.searchBar.placeholder = "Search for places"
self.searchController!.hidesNavigationBarDuringPresentation = false
self.searchController!.dimsBackgroundDuringPresentation = false;
self.searchController!.searchBar.searchBarStyle = UISearchBarStyle.Prominent
self.searchController!.delegate = self
self.searchController!.searchBar.delegate = self
self.definesPresentationContext = true
self.searchController!.searchBar.sizeToFit()
self.navigationItem.titleView = self.searchController!.searchBar
This might help
Replace
self.searchController = UISearchController(searchResultsController: tvController)
With
self.searchController = UISearchController(searchResultsController: nil)

UISearchController in navigationBar

I'm trying to use the new UISearchController in my tableViewController.
However I'm a bit confused on how it can move up in the navigationController when I press the searchBar like it did with the old searchDisplayController?
At the moment it just stay in the tableHeader.
Here is my code:
self.teamSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.showsScopeBar = true
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
Controller:
When I click on searchbar:
You can place the UISearchBar of UISearchController in the navigation bar instead of table header
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;
Swift version:
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar
self.definesPresentationContext = true
It has to do with the navigation bar.
func willPresentSearchController(searchController: UISearchController) {
self.navigationController?.navigationBar.translucent = true
}
func willDismissSearchController(searchController: UISearchController) {
self.navigationController?.navigationBar.translucent = false
}
If you do this, then you will be able to add it to the table view header AND get the baked-in search controller animation!

UISearchController makes the controller black

I am using UISearchController in iOS 8 with the following intializaiton in viewDidLoad of a view controller embedded in a tab controller
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchBar = _searchController.searchBar;
[_searchController.searchBar sizeToFit];
_searchController.searchBar.delegate = self;
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;
_shopsTable.tableHeaderView = _searchController.searchBar;
I've implemented
- (void) updateSearchResultsForSearchController:(UISearchController *)searchController and (void)filterContentForSearchText:(NSString *)searchText
and the search works, the tableview gets updated properly, etc.
But!
If I switch tabs while the searchcontroller is active (just tapping the search bar or with some text) to a different tab, and then go back to teh search tab, I get a blank screen with only the search bar, like this
In this case, I search for things that start with lar, which did return results and displayed them correcly. But if I switch tabs, and return to the search tab I get a blank screen like this. The only way the controller returns to its original state is if I do _searchController.active = NO. But if the user wants to keep that search active, I can't just deactivate it.
I am sure I am missing something, but since there isn't much to do in UISeachController, I can't figure out what is causing this..
Try self.definesPresentationContext = YES; instead of NO. Here is how I setup my UISearchController, but I haven't done it this way in a UITabBarController before.
func setupSearchController() {
let resultsController = UIStoryboard(name: "ATPageTableViewController", bundle: nil).instantiateViewControllerWithIdentifier("ATPageTableSearchResultsViewController") as! ATPageTableSearchResultsViewController
searchController = UISearchController(searchResultsController: resultsController)
searchController.delegate = self
resultsController.delegate = self
resultsController.cellIdentifier = ATDataSetItemTableViewCellIdentifier;
resultsController.table = self.table!
searchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
definesPresentationContext = true
}
Write the below code in viewDidLoad mehtod.
self.definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = true

scopeBar don't show under UISearchBar when it is active for the first time

I programmatically created a UISearchController, and set the scope button titles in viewDidLoad
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
[searchResultsController.tableView registerNib:musicNib forCellReuseIdentifier:#"MusicCell"];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
UISearchBar *searchBar = self.searchController.searchBar;
searchBar.scopeButtonTitles = #[#"Album", #"Artist", #"Song"];
searchBar.showsScopeBar = YES;
searchBar.selectedScopeButtonIndex = 0;
searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
But when I search in search bar when view loaded. the scope bar don't show up. The second I search in search bar, the scope bar shows. What's going on?
Try this:
searchBarDisplayController.searchBar.showsScopeBar = NO;
searchBarDisplayController.searchBar.scopeButtonTitles = nil;
Based on your comment, put the above code in searchDisplayControllerWillBeginSearch: delegate method. That should work.
searchController.delegate = self
// in the delegate...
func willPresentSearchController(_ searchController: UISearchController) {
searchController.searchBar.showsScopeBar = true
searchController.searchBar.sizeToFit()
}
func willDismissSearchController(_ searchController: UISearchController) {
searchController.searchBar.showsScopeBar = false
searchController.searchBar.sizeToFit()
}
For iOS 16 and upper try this:
searchController.scopeBarActivation = .onSearchActivation
You can remove searchBar.showsScopeBar = YES;

Resources