问题
I have hidden my navigation bar in my table view controller, but after selecting the search bar and then dismissing it with Cancel it shows the navigation bar again. I tried
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
self.navigationController.navigationBar.hidden = YES;
}
But it did not work, it did hide the navigation bar from the detail view though, but that was not desired.
In storyboard I have a tabBarController --> navigationController --> tableView --> detailview.
EDITED:
My code:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
searchResults = [_truckArray filteredArrayUsingPredicate:resultPredicate];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
} else {
return [_sectionTitles count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
if (section == 0) {
[_sectionTitles replaceObjectAtIndex:0 withObject:@""];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
return [searchResults count];
} else {
return 0;
}
} else {
NSString *sectionName = [_sectionTitles objectAtIndex:section];
NSArray *sectionArray = [_sections objectForKey:sectionName];
return [sectionArray count];
}
}
In cellForRowAtIndexPath:
Item *item = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
item = [searchResults objectAtIndex:indexPath.row];
} else {
NSString *sectionTitle = [_sectionTitles objectAtIndex:indexPath.section];
NSArray *sectionArray = [_sections objectForKey:sectionTitle];
item = [sectionArray objectAtIndex:indexPath.row];
}
回答1:
If when you dismiss the search bar try to see if you are calling again the viewDidAppear or viewWillAppear method of your tableView an in there call your code to hide the navigation bar.
I know for sure that when you show a modal view, when you dismiss the view the viewDidAppear method is called, maybe here you have the same behavior.
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
I hope this helps.
Happy coding.
回答2:
Make sure you set <UISearchBarDelegate>
in your view controller
or by ref..self.searchBarRef.delegate=self
;
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if([searchText isEqualToString:@""] || searchText==nil) {
self.navigationController.navigationBar.hidden = YES;
}
}
//OR use below delegate method
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
self.navigationController.navigationBar.hidden = YES;
}
Keep below code at viewWillAppear or ViewDidLoad and check it.
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
Hope it fixes the issue...!
回答3:
What about hiding the navigation bar on searchBarTextDidBeginEditing
and enabling it again searchBarTextDidEndEditing
, you just have to animate the frame of navigation bar to hide and unhide .
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[UIView animateWithDuration:0.2 animations:^{
self.navigationController.navigationBar.frame = CGRectMake(0, -64, 320, self.navigationController.navigationBar.frame.size.height);
}];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
[UIView animateWithDuration:0.2 animations:^{
self.navigationController.navigationBar.frame = CGRectMake(0, 64, 320, self.navigationController.navigationBar.frame.size.height);
}];
}
NOTE:-I have added hardcoded value for Y axis and width, it can be easily calculated as per navigation-bar frame and status-bar frame.
回答4:
//Search Controller
-(void)initSearchController{
self.isSearchActive=false;
self.searchBarTimeline=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[self.view addSubview:self.searchBarTimeline];
self.searchBarTimeline.delegate=self;
self.searchBarTimeline.placeholder=@"Search";
self.searchBarTimeline.searchBarStyle=UISearchBarStyleMinimal;
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blackColor]];
}
#pragma mark - Tableview search
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[self.searchBarTimeline setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
[self.searchBarTimeline setShowsCancelButton:NO animated:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
self.isSearchActive=true;
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"timecontact.userName contains[c] %@ OR timecontact.userSummary contains[c] %@", searchText,searchText];
searchTimelineResults = [[[[UIAppDelegate coreDataHelper] timelineFetchedResultsController] fetchedObjects] filteredArrayUsingPredicate:resultPredicate];
if(searchTimelineResults.count==0){
self.isSearchActive=false;
}
else{
self.isSearchActive=true;
}
[self.tableView reloadData];
}
Play with flag isSearchActive and reload data of tableview
来源:https://stackoverflow.com/questions/30300457/hide-navigation-bar-after-using-search-bar