使用搜索栏后隐藏导航栏 [英] Hide navigation bar after using search bar

查看:95
本文介绍了使用搜索栏后隐藏导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将导航栏隐藏在表视图控制器中,但是选择搜索栏然后单击取消"将其关闭后,它将再次显示导航栏.我尝试过

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.

在情节提要中,我有一个tabBarController-> navigationController-> tableView-> detailview.

In storyboard I have a 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];
    }
}

在cellForRowAtIndexPath中:

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];
}

推荐答案

如果关闭搜索栏,请尝试查看是否再次调用表的viewDidAppear或viewWillAppear方法.在其中查看代码,以隐藏导航酒吧.

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.

我肯定知道,当您显示模式视图时,当您关闭视图时,将调用viewDidAppear方法,也许在这里您具有相同的行为.

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];
}

我希望这会有所帮助.

快乐的编码.

这篇关于使用搜索栏后隐藏导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆