如何从桌面视图控制器的屏幕顶部显示UISearchBar

我正在重复如何Facebook Messenger应用程序显示UISearchBar。 当您点击导航栏中的左边栏button时,UISearchBar会从屏幕顶部向下出现/生成animation,当您取消时,它会从其起源处向上消失。

我曾经有我的UISearchBar作为我的表视图的头(故事板)中的默认设置,但现在我想要做我上面说的,但我不知道从哪里开始。 我的search显示器控制器仍然在我的故事板,但我从故事板的tableView控制器中删除了searchBar。

我感谢提供的任何帮助!

省略我自己的项目的细节,我做了同样的事情,看起来如下:

static CGFloat searchBarHeight = 64.0; @implementation { NSArray *_tableData; // active list of data to show in table at the moment NSMutableArray *_filteredContacts; // filtered list while search is active NSArray *_allContacts; // initial list of all data without search UIView* _searchBarWrapper; UIView *_shadowView; UISearchBar *_searchBar; } - (void)loadView { [super loadView]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showSearchBar:)]; _searchBarWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, -searchBarHeight, self.navigationController.view.bounds.size.width, searchBarHeight)]; _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20.0, _searchBarContainer.bounds.size.width, searchBarHeight - 20.0)]; [_searchBarContainer addSubview:_searchBar]; [self.navigationController.view addSubview:_searchBarContainer]; _shadowView = [[UIView alloc] initWithFrame:self.navigationController.view.bounds]; _shadowView.backgroundColor = [UIColor blackColor]; _shadowView.alpha = 0; [self.navigationController.view addSubview:_shadowView]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideSearchBar:)]; UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideSearchBar:)]; [_shadowView addGestureRecognizer:tapGesture]; [_shadowView addGestureRecognizer:swipeGesture]; //_searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self]; //_searchController.delegate = self; //_searchController.searchResultsDataSource = self; //_searchController.searchResultsDelegate = self; } - (void)showSearchBar:(id)sender { [_searchBar becomeFirstResponder]; [UIView animateWithDuration:0.25 animations:^{ _searchBarWrapper.center = CGPointMake(_searchBar.center.x, searchBarHeight/2.0); _shadowView.alpha = 0.5; }]; } - (void)hideSearchBar:(id)sender { _searchBar.text = nil; [_tableView reloadData]; [UIView animateWithDuration:0.25 animations:^{ _searchBarWrapper.center = CGPointMake(_searchBar.center.x, -searchBarHeight/2.0); _shadowView.alpha = 0.0; }]; [_searchBar resignFirstResponder]; [_tableView reloadData]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { _tableData = _searchBar.isFirstResponder ? _filteredContacts : _allContacts; .................... } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if (searchText.length) { _filteredContacts = ....; _shadowView.alpha = 0.0; [_tableView reloadData]; } else { _shadowView.alpha = 0.5; } } - (void)searchBarCancelButtonClicked:(UISearchBar*)searchBar { [self hideSearchBar:searchBar]; } 

注意:这个构造的一般行为类似于UISearchDisplayController ,唯一的区别是我们使用同一个表来显示所有结果和过滤结果。 我们还需要人造阴影视图。

这是我的初始代码,它的行为与FB人员视图控制器一致。 在你的评论之后,我写了标准的UISearchDisplayControllerembedded,但之后评论它,因为它打破了所需的用户界面。 改变其animation行为是不可能的。

看看苹果的UICatalog示例代码。 基本上只是呈现你的UISearchController模态,你可以免费获得下拉animation。

Facebook似乎隐藏了导航栏,并在顶部显示了search栏和search显示控制器。 试试用[self.navigationController setNavigationBarHidden:YES animated:NO];隐藏导航栏[self.navigationController setNavigationBarHidden:YES animated:NO]; 并将您的search栏和search显示展示为您的tableView的子视图。 (我假设你知道“ search栏和search显示控制器 ”对象是如何工作的)

另一种方法是避免UITableViewController并在UIViewController中添加一个UITableView,并在导航栏下隐藏search栏,所以当你隐藏导航栏时,search栏是可见的。

您也可以将UISearchBar设置为UINavigationBar的titleView ,如此处所述 。