gpt4 book ai didi

ios - 如何从屏幕顶部为 TableView Controller 显示 UISearchBar

转载 作者:可可西里 更新时间:2023-11-01 04:25:28 24 4
gpt4 key购买 nike

我想复制 Facebook Messenger 应用程序显示 UISearchBar 的方式。当您点击 navigationBar 中的 leftBarButtonItem 时,UISearchBar 会从屏幕顶部向下显示/动画,当您取消时,它只会向上消失。

我曾经将我的 UISearchBar 作为我的 TableView 标题(在 Storyboard中)的默认设置,但现在我想做我上面所说的,但我不确定从哪里开始。我的搜索显示 Controller 仍在我的 Storyboard 中,但我已从 Storyboard 中的 tableView Controller 中删除了 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,唯一的区别是我们使用同一个表来显示所有结果和过滤结果。我们还需要人工阴影 View 。

这是我的初始代码,它的行为与 FB People View Controller 一致。在您发表评论后,我编写了标准的 UISearchDisplayController 嵌入,但在将其注释掉之后因为它破坏了所需的 UI。改变它的动画行为是不可能的。

关于ios - 如何从屏幕顶部为 TableView Controller 显示 UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21194473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com