【问题标题】:How to show a UISearchBar from top of screen for table view controller如何从屏幕顶部为表格视图控制器显示 UISearchBar
【发布时间】:2014-02-07 06:57:00
【问题描述】:

我希望复制 Facebook Messenger 应用程序显示 UISearchBar 的方式。当您点击导航栏中的 leftBarButtonItem 时,UISearchBar 会从屏幕顶部向下出现/动画,当您取消时,它会从其起源的位置向上消失。

我曾经将 UISearchBar 作为我的表视图标题(在故事板中)中的默认设置,但现在我想做我上面所说的,但我不确定从哪里开始。我的搜索显示控制器仍在我的情节提要中,但我已从情节提要的 tableView 控制器中删除了 searchBar。

感谢您提供的任何帮助!

【问题讨论】:

  • 只是一个建议 - 不确定。也许您可以尝试添加一个子视图,当您点击 leftBarButtonItem 时会出现该子视图。
  • 我试过了,但问题是将搜索显示的搜索栏添加到该子视图;它实际上并没有被添加。
  • 可以发布屏幕截图或示例项目

标签: ios objective-c ios7 uinavigationcontroller uisearchbar


【解决方案1】:

Facebook 似乎隐藏了导航栏并在顶部显示了搜索栏和搜索显示控制器。尝试使用[self.navigationController setNavigationBarHidden:YES animated:NO]; 隐藏导航栏,并将搜索栏和搜索显示显示为 tableView 的 childView。 (我假设你知道“Search Bar and Search Display Controller”对象是如何工作的)

另一种方法是避免 UITableViewController 并在 UIViewController 中添加 UITableView 并将搜索栏隐藏在导航栏下,这样当您隐藏导航栏时,搜索栏是可见的。

您也可以将 UISearchBar 设置为 UINavigationBar 的titleView 为described here。

【讨论】:

  • 你确定吗?仔细看,我没有看到他们隐藏了导航栏。
【解决方案2】:

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

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 嵌入,但在将其注释掉后,因为它破坏了所需的 UI。无法更改其动画行为。

【讨论】:

  • 您能说明一下您的 searchBarDisplayController 及其各自的搜索条码在哪里吗?
  • 其实你甚至不需要 hideSearchBar。 SearchDisplayController 全部用于隐藏。
  • 今晚晚些时候我会试试这个。别担心,如果它对我有用,我会接受你的回答。
  • 请查看我的扩展答案。
  • 我想不通,主要是因为未声明的变量:_searchBarContainer
【解决方案3】:

查看 Apple 的 UICatalog 示例代码。基本上只需以模态方式展示您的UISearchController,您就可以免费获得下拉动画。

【讨论】:

  • 天哪,你杀了它。我想将 definesPresentationContext 设置添加到 false 使 navigationBar 没有动画效果,看起来很棒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
相关资源
最近更新 更多