Do not show searchResultsTableView before search button clicked - ios

I'm trying to solve this problem with no luck. I have a search bar and search a display controller inside table view controller.
Everything works fine - I make the server call to search the endpoint when user clicks the "Search" button and display the results. But the problem is I do not want to show No results in the table view before user clicks Search.
Instead of that I prefer to show default dark overlay.

You could return 1 in numberOfRowsInSection while the query is in progress and leave the dummy cell empty or set its hidden property to YES so it is not visible. So you have do not have "No Results" showing up.
Also for making it a dark overlay try something like this:
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView{
tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"default.png"]];
}
More information: Link!

Related

UISearchController search bar text disappears when losing focus

I'm using the UISearchBar of a UISearchController in the header of a UITableView. My searching works OK in that when I enter some text in the search bar's field, all the relevant methods are called and the table view shows the correct search results as expected.
The problem comes when I want to tap on one of the results to do something with it. As soon as I tap on one of the table view cells, the field in the search bar becomes empty. Since the search text is now empty, the search results are updated and the table view reloads with zero results. So the tap on the table cell does nothing, because the table cell no longer exists.
I can work around most of this problem by updating my updateSearchResultsForSearchController: method to not update the search results when the search string is empty. And this works fine. But in any case, I do still want the search text to remain in the search field, and this is the crux of my question...
Why is my search text disappearing from the search field, and how can I prevent the search field from becoming empty when I tap elsewhere?
Here's how I set up the search bar and search controller:
- (void)viewDidLoad {
[super viewDidLoad];
searcher = [[UISearchController alloc] initWithSearchResultsController:nil];
searcher.searchResultsUpdater = self;
searcher.delegate = self;
searcher.definesPresentationContext = YES;
resultsTable.tableHeaderView = searcher.searchBar;
EDIT: I notice that the table view cells are dimmed while the search bar has focus. So I think that the search controller is capturing all touches and perhaps handles them as 'cancel' actions. If so, how do I stop it cancelling in this way?
Figured it out. I needed to add:
searcher.obscuresBackgroundDuringPresentation = NO;

How to trigger the "cancel search" when using only a UISearchBar?

I have a table view with a header that displays a UISegmentedControl and a UISearchBar. Keep in mind that I am not using a UISearchController. When I have searched the current list, and I toggle a different segment, I want to cancel the search and reload the list with the new data.
Currently I am manually clearing the search text, animating the cancel button and hiding the keyboard. But I am also doing that inside my searchBarCancelButtonClicked method.
Is there a way to programmatically cancel the search (clearing the text, hiding the keyboard, and hiding the cancel button) when only using a UISearchBar?
No, there's no way to automatically clear the text field, hide the keyboard, etc. Even though it seems like common functionality, it's up to each developer to decide how to implement it. Apple just provides the ability to catch the event when the user taps the Cancel button.
Typically, I'll create a local function in the view controller like:
-(void)clearSearchBar:(UISearchBar *)searchBar {
searchBar.Text = #"";
[searchBar resignFirstResponder];
...
}
Then, in searchBarCancelButtonClicked: (assuming you've created an IBOutlet for the search bar), you can do this:
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self clearSearchBar:self.searchBar];
}
You can then also call clearSearchBar: anywhere else you need to clear it.
Obviously, there's room to use categories here to avoid duplicating code.
You could also just call searchBarCancelButtonClicked: like this:
[self.searchBar.delegate searchBarCancelButtonClicked:self.searchBar];

Search freezes in UISearchBar with searchDisplayController

I have a UISearchDisplayController working nicely with a UISearchBar. shouldReloadTableForSearchString correctly returns results as the user types, but when the user hits the Search button on the keyboard and searchBarSearchButtonClicked is triggered, something odd happens after the final search results correctly display and the user hits Cancel on the search bar. Once the user reactivates search, the search opens but the cursor doesn't blink, the search results don't display and the keyboard looks locked on every keypress as shown in the attached image. There should be results for "Test", but nothing is happening and something seems to be held up b/c the last pressed key ("T") isn't going back down to the keyboard.
I'm holding off on including code b/c there are a lot of moving parts, but if any code would help, i can quickly include it. Thanks for your help.
UPDATE: it's not just after the user submits the query - if they've touched the resulting searchDisplayController.searchResultsTableView after inputting a query to scroll down through the results, and then hit Cancel, they'll see the frozen cursor and broken search once they activate the searchBar again (same image applies).
SECOND UPDATE: this only happens when I hide / unhide the navbar while search is active in viewWillLayoutSubviews. Any ideas? Really appreciate any help!
-(void)viewWillLayoutSubviews {
if (self.searchDisplayController.isActive && !profileSelected) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
else {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
LAST UPDATE: when hide/unhide code is placed into searchDisplayControllerWillBeginSearch and searchDisplayControllerWillHideSearch respectively, the searchBar incorrectly unhides 20px below navbar:
fixed it by adding this code:
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.searchDisplayController.searchBar becomeFirstResponder];
}
UISearchDisplayController is using UITableView to show search results. Whenever you scroll or new cells are loaded, viewWillLayoutSubviews is getting called. This is causing the app to freeze. Try moving you setNavigationBarHidden code in a separate method triggered by some other action.

UISearchController not displaying "no results" in background and how to alter dimmed background

I have a UISearchController and the "No Results" is not showing during searches. How do I get this to appear in the UISearchController background when nothing is found? I would also like the search controller to behave like the UISearchDisplayController where the background dims when you click the searchBar initially, but undims when typing begins. I'm not sure how to do this.
For the "No Results" question, Daniel Amitay has a really good answer on another question. https://stackoverflow.com/a/4840621/4525245
For the dimming question, if I understand you right, you could just have a translucent image that adds over the UItableView after the searchBar is tapped and have it remove after the user taps the keyboard. Might not be elegant but it should work.

Revise background color between UISearchBar and keyboard when the search filed get focus

When the search field get focus, there is a black transparent view between search bar and keyboard, just like in iPhone email app, when you want to search an email, you click the search field first, there is a black transparent view appear. When you enter some words, there will be a result table view instead. And if you tap the view, the key board will close and the search filed will lose focus.
Now, I want to change the color of this view, for example to red transparent view. I have try it by using inputAccessoryView. But there still has a black one under it and I have to add tap action for it.
Is there any property can edit it?
I find the answer by myself.
for (UIView *subview in searchBar.superview.subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchDisplayControllerContainerView")])
{
// in iOS7, this view is the view I want;
}
if ([subview isKindOfClass:NSClassFromString(#"UIControl")])
{
// before iOS7, this view is what I want;
}
}
It seems to be very secretly, I don't know whether Apple allows or supports us to change it. Maybe it doesn't encourage us to do like above. But it can solve my problem.

Resources