使用搜索栏后隐藏导航栏

pou*_*iot 2 objective-c hide uinavigationbar uisearchbar ios

我已将我的导航栏隐藏在我的表视图控制器中,但在选择搜索栏然后使用取消将其解除后,它再次显示导航栏.我试过了

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    self.navigationController.navigationBar.hidden = YES;
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用,它确实隐藏了导航栏的细节视图,但这不是所希望的.

在storyboard中我有一个tabBarController - > navigationController - > tableView - > detailview.

编辑:

我的代码:

-(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];
    }
}
Run Code Online (Sandbox Code Playgroud)

在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];
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*coz 6

如果当您关闭搜索栏时尝试查看是否再次调用tableView的viewDidAppear或viewWillAppear方法,那么调用代码隐藏导航栏.

我确信当你显示一个模态视图时,当你关闭视图时会调用viewDidAppear方法,也许你在这里有相同的行为.

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.

快乐的编码.