Опуская подробности моего проекта, где я делаю то же самое, он выглядит следующим образом:
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 People. После вашего комментария я написал стандарт UISearchDisplayController, но после прокомментировал это, потому что он разбивает желаемый пользовательский интерфейс. Невозможно изменить его анимационное поведение.
Просто предложение - не уверен. Возможно, вы могли бы попробовать добавить subview, который появляется при нажатии на leftBarButtonItem. – Dinesh
Я пробовал это, но проблема заключается в добавлении строки поиска на экран поиска к этому подвью; он фактически не добавляется. – klcjr89
можно отправить снимок экрана или образец проекта – codercat