ios objective-c tableView searchBar - ios

I have a hidden searchBar in a tableView header:
- (void)viewWillAppear:(BOOL)animated {
CGRect newBounds = self.tableView.bounds;
newBounds.origin.y = newBounds.origin.y + self.searchBar.bounds.size.height;
self.tableView.bounds = newBounds;
}
I also have a magnifying-glass icon button that reveals the search bar:
- (IBAction)showSearchBar:(id)sender {
[self.searchDisplayController setActive:YES animated:YES];
[self performSelector:#selector(showKeyboard) withObject:nil afterDelay:0.1];
}
Problem is, when I pull down the table (to reload data) - it also shows the search bar.
I only want the searchBar to become visible when the magnifying-glass icon gets tapped.
I am not really sure, how to address this problem?
Am I hiding it the wrong way in the first place?
Thanks,
Added a screenshot to make my question more clear:
searchbar appears when pulling down

I don't tried that out, but I can image that you just have to hide it. for example:
[[self.searchDisplayController view] setHidden:YES]

Related

UISearchDisplayController table view overlapping search bar

I have a UITableViewController subclass, displayed in a modal view on an iPad. The view controller has a UISearchDisplayController subclass with a UISearchBar included in the header view of the table.
The subclassed UISearchDisplayController is called NoAnimationSearchDisplayController and I have overridden the - (void)setActive:(BOOL)visible animated:(BOOL)animated
method to prevent the search bar from animating into the navigation bar when it's set to active. The method override is below...
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if (self.active == visible) {
return;
}
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
if (visible) {
[self.searchBar becomeFirstResponder];
} else {
[self.searchBar resignFirstResponder];
}
}
The problem I'm having, is that when I have searched, and results are displayed in my search display controller's table view, everything looks fine untill i try to scroll down the list, at this point the content within the cells appears above the search bar, as shown in the following screen:
The search bar is set to UISearchBarStyleMinimal and transparency is enabled. Can anyone let me know how to stop this content overlapping the search bar? Ideally the content would disappear under the search bar as if it was the end of the view.
The answer was to manually change the frame of the table view provided by the UIsearchDisplayController in the appropriate delegate method...
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
/**
* Remove content inset automatically set by UISearchDisplayController as we are forcing the
* search bar to stay in the header view of the table, and not go into the navigation bar.
*/
[tableView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
/**
* Recalculate the bounds of our table view to account for the additional 44 pixels taken up by
* the search bar, but store this in an iVar to make sure we only adjust the frame once. If we
* don't store it in an iVar it adjusts the frame for every search.
*/
if (CGRectIsEmpty(_searchTableViewRect)) {
CGRect tableViewFrame = tableView.frame;
tableViewFrame.origin.y = tableViewFrame.origin.y + 44;
tableViewFrame.size.height = tableViewFrame.size.height - 44;
_searchTableViewRect = tableViewFrame;
}
[tableView setFrame:_searchTableViewRect];
}
Be sure the search bar is not translucent enabled ( on storyboard/xib ) or by code.
Then make the background to white or whatever color you want.

Hiding UISearchBar in iOS 7 crops top row

I am calling the following function to successfully hide my search bar in viewDidLoad:
- (void)hideSearchBar {
CGRect newBounds = self.tableView.bounds;
newBounds.origin.y = newBounds.origin.y + _searchBar.bounds.size.height;
self.tableView.bounds = newBounds;
}
but if I call the exact same function in (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar the top row of my table view becomes overlapped with the navigation bar. Why is this overlap only happening when calling the hide function from searchBarTextDidEndEditing?
An answer from this question helped me realize this is somehow related to the nav bar being translucent. When I set the nav bar translucent to NO I had the issue. When I stopped making it NO, it works fine.

How to show/hide a search bar inside a navigation bar (iOS 7) as in Apple's Calendar app?

I want to use a search bar button in a navigation bar to show a search bar just like Apple do in the Calendar app. The cancel button would dismiss the search bar and return the navigation bar to its former state, bar buttons, title, etc.
Using the iOS 7 property:
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
puts the search bar into the navigation bar just fine. My problem is trying to have it appear conditionally on the press of a bar button. I've tried firing this line from my button's action but no go. I've tried setActive:Animated: on the searchDisplayController
self.searchDisplayController.active = YES;
but no luck either. Any ideas or help would be appreciated.
I'm not sure if you notice, but on the Apple's calendar app when you press the search icon, it open a new UITableView with search bar. If this is what you want to do, you will have a create a UIViewController with a UITableView and a UISearchBar which inside that tableView you will be filtering the content.
If I was you, I will just hide the UISearchBar and call it whenever is needed with the button to show up.
This might work as well. Just give it a try and let me know:
In your viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
// scroll search bar out of sight
CGRect newBounds = self.tableView.bounds;
if (self.tableView.bounds.origin.y < 44) {
newBounds.origin.y = newBounds.origin.y + self.searchBar.bounds.size.height;
self.tableView.bounds = newBounds;
}
// new for iOS 7
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:0 animated:YES];}
Now that is hidden, call this to hide it again when the search is done:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self viewWillAppear:YES];
}
and to show the searchBar with a button, then:
- (IBAction)displaySearchBar:(id)sender {
// makes the search bar visible
[self.searchBar becomeFirstResponder];
}
In iOS 8 you can simply present a UISearchController and you will get the same animation as Calendar.app. Check out Apple's UICatalog sample code for an example.

Search Bar Cancel Button don´t work in ios 7 sometimes

Cancel button in search bar don´t work in iOS 7 when search bar is initially hidden.
I follow this tutorial to create a search bar in tableview:
raywenderlich tutorial
There are a example project in this tutorial, is better use this project than my explanation :)
In iOS 5 and 6 works fine.
I have reviewed all delegates.
There are two possibilities. The first is to press the button when the bar is hidden, the second is to press the button when the bar is displayed (moving the table down with a gesture you can see the search bar)
If search bar is hidden initially cancel button don´t work, it don't call calcel delegate method:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
Sorry I can not explain it better.
Thanks
I googled all over internet and couldn't find a solution. so i changed the UItableview behaviour.
instead of [searchBar becomeFirstResponder]; I scroll down the tableview.
- (IBAction)goToSearch:(id)sender {
scroll down to show the table.
// CGRect newBounds = self.tableView.bounds;
// newBounds.origin.y =0;
//
// self.tableView.bounds = newBounds;
//[searchBar becomeFirstResponder];
CGPoint contentOffset=self.tableView.contentOffset;
contentOffset.y=0;
[self.tableView setContentOffset:contentOffset animated:YES];
}
in my ViewDidload:
// CGRect newBounds = self.tableView.bounds;
// newBounds.origin.y = newBounds.origin.y + searchBar.bounds.size.height;
// self.tableView.bounds = newBounds;
CGPoint contentOffset=self.tableView.contentOffset;
contentOffset.y=self.tableView.bounds.origin.y + searchBar.bounds.size.height;
self.tableView.contentOffset=contentOffset;
If found for some reasons, in iOS 7 , change table view bounds cause search bar to disappear.
Hope that helps.
That code work for me on iOS7:
- (IBAction)goToSearch:(id)sender {
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[candySearchBar becomeFirstResponder];
}
put this code in your project it will work i have tested and it is working correctly
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchbar
{
[searchbar resignFirstResponder];
for (UIView *possibleButton in searchbar.subviews)
{
if ([possibleButton isKindOfClass:[UIButton class]])
{
UIButton *cancelButton = (UIButton*)possibleButton;
cancelButton.enabled = YES;
break;
}
}
}
This problem seems to come from the new behaviour of the translucent property in a navigation bar.
Since iOS 7 navigation bars are translucent by default. And it looks like it's overlapping the search bar when you display it after pressing a button.
Try to set in your controller:
float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (osVersion >= 7.0)
{
self.navigationController.navigationBar.translucent = NO;
}
This should quickly solve the problem.
But I think for a better fix you should see the iOS 7 transition guide where they explain how to handle translucent navigation bars.
Hope that helps.
I assume you have set _searchBar.delegate = self and implemented UISearchBarDelegate in your class.
This is how you do it:
- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ // called when cancel button pressed
searchBar.text = nil;
//hide cancel button
[_searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
}

Why my UISearchBar`s frame is changed by the UISearchDisplayController

I write UISearchBar in my TopBar.m like this:
_tempSearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(44, 0, 320 - 44, 43)];
_tempSearchBar.barStyle=UIBarStyleDefault;
_tempSearchBar.placeholder=#"搜索";
[self addSubview:_tempSearchBar];
the result is like this, it is right.
and then I write UISearchDisplayController in another class like this:
_topBar.tempSearchBar.delegate = self;
_searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_topBar.tempSearchBar contentsController:self];
[_searchDisplayController setDelegate:self];
[_searchDisplayController setSearchResultsDataSource:self];
the UISearchBarDelegate is like this:
#pragma mark -
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[_searchDisplayController setActive:YES animated:YES];
}
when I click the UISearchBar , it show like this , the searchBar`s frame is changed.why?
when I cancel the UISearchDisplayController it is like this :
why the frame is changed? The width is changed from 320-44 to 320 by the UISearchDisplayController?
thanks.
UISearchDisplayController expands the search bar to the width of its superview. The simplest solution that I have found is to place the search bar inside another UIView that has the width I am looking for.
The searchBar's frame is changed by the UIKit, so I changed the searchBar's frame back myself.
I changed the searchBar's frame in the below delegate.
One is UISearchBar's delegate:
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[searchBar setFrame:CGRectMake(44, 0, 320 - 44, 43)];
}
Another is UISearchDisplayController's delegate:
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
[controller.searchBar setFrame:CGRectMake(44, 0, 320 - 44, 43)];
[self.searchDisplayController.searchResultsTableView setDelegate:self];
}
It can work and I can get the right frame, but when I click the searchBar it will shake a little.
It is not the best way to do it, but it can work. Does anyone have a better method?
Update:
I have debugged the UISearchBar and UISearchDisplayController for a few hours, but it has a little bug: When I endEditing the searchBar's width will become 320px, and then will become my width. I can not change the cancelButton's background color. So I wrote a custom SearchDisplayController, with a UISearchBar property and a UITableView property. It works well for me.
Call the delegate method
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
[controller.searchBar setFrame:CGRectMake(0,31, 320 , 43)];
[self.searchDisplayController.searchResultsTableView setDelegate:self];
}
You can handle the cancel button of searchBar using - (void)setShowsCancelButton:animated:
If you do not want to show cancel button (as cancel button will change the frame of searchBar)
just write this in delegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
Updated:
An only possible solution seems to be finding the cancel button from SearchBar view hierarchy and hiding it.
for (UIView *possibleButton in searchBar.subviews)
{
if ([possibleButton isKindOfClass:[UIButton class]])
{
UIButton *cancelButton = (UIButton*)possibleButton;
cancelButton.hidden = YES;
break;
}
}

Resources