在推送另一个视图控制器后,searchBar不会隐藏

时间:2014-12-29 12:12:41

标签: ios objective-c iphone uitableview uisearchbar

我在表格的标题视图中创建了一个搜索栏:

self.searchResults = [NSMutableArray arrayWithCapacity:[self.list count]];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.barTintColor = [UIColor clearColor];
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
[self.searchController.searchBar becomeFirstResponder];

当点击tableview中的某一行时,将推送一个新的视图控制器。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"HEY ROW %ld", indexPath.row+1);
    ZLContainerViewController *controller = [[ZLContainerViewController alloc]init]; // a container for UIPage
    [self.navigationController pushViewController:controller animated:YES];    
}

但是,在按下新视图控制器后,搜索栏仍然存在。我该如何解决这个问题?

非常感谢任何建议!

2 个答案:

答案 0 :(得分:2)

这两个UISearchBarDelegate委托方法中的一个应该解决您的问题

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.searchBar resignFirstResponder];
}

答案 1 :(得分:0)

使用viewDidLoad中的以下代码。

<强>夫特:

self.definesPresentationContext = true

<强>目标-C:

self.definesPresentationContext = YES;

这解决了我的问题。