使用搜索栏后隐藏导航栏

时间:2015-05-18 10:12:56

标签: ios objective-c uinavigationbar hide uisearchbar

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

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    self.navigationController.navigationBar.hidden = YES;
}

但是它不起作用,它确实将导航栏隐藏在详细视图中,但这不是所希望的。

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

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

4 个答案:

答案 0 :(得分: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];
}

我希望这会有所帮助。

快乐的编码。

答案 1 :(得分:1)

确保在视图控制器中设置<UISearchBarDelegate>

或参考.. self.searchBarRef.delegate=self;

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
   if([searchText isEqualToString:@""] || searchText==nil) {
        self.navigationController.navigationBar.hidden = YES;
    }
}

//或者使用下面的委托方法

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    self.navigationController.navigationBar.hidden = YES;
}

在viewWillAppear或ViewDidLoad下方保留以下代码并进行检查。

if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;

希望它能解决问题......!

答案 2 :(得分:0)

如果隐藏searchBarTextDidBeginEditing上的导航栏并再次启用它searchBarTextDidEndEditing,您只需要为导航栏框架设置动画以隐藏和取消隐藏。

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

注意: - 我已经为Y轴和宽度添加了硬编码值,可以根据导航栏框架和状态栏框架轻松计算。

答案 3 :(得分:0)

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

使用标记isSearchActive播放并重新加载tableview的数据