klc*_*r89 5 objective-c uinavigationcontroller uisearchbar ios ios7
我想复制Facebook Messenger应用程序显示UISearchBar的方式.当你点击navigationBar中的leftBarButtonItem时,UISearchBar会从屏幕顶部向下显示/动画,当你取消时,它会从它的起源处向上消失.
我曾经把我的UISearchBar作为我的表格视图标题中的默认设置(在故事板中),但现在我想要做我上面所说的但我不知道从哪里开始.我的搜索显示器控制器仍在我的故事板中,但我已从故事板中的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];
}
Run Code Online (Sandbox Code Playgroud)
注意:此构造的一般行为类似于UISearchDisplayController,唯一的区别是我们使用同一个表来显示所有结果和过滤结果。我们还需要人工阴影视图。
这是我最初的代码,其行为与 FB People 视图控制器一致。在您发表评论之后,我编写了标准UISearchDisplayController嵌入,但在注释掉之后,因为它破坏了所需的 UI。改变它的动画行为是不可能的。
| 归档时间: |
|
| 查看次数: |
5030 次 |
| 最近记录: |