隐藏UITableView搜索栏 [英] Hide UITableView search bar

查看:58
本文介绍了隐藏UITableView搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableViewController,它以标准方式设置了UISearchDisplayController(在tableView内有搜索栏).我希望搜索栏开始隐藏-真正隐藏,而不仅仅是滚动如在此解决方案中一样.然后,我想在用户按下按钮时显示搜索用户界面,并在用户选择搜索中找到的项之一后再次将其隐藏(真正隐藏).

I have a UITableViewController with a UISearchDisplayController setup in the standard way (with the search bar inside the tableView). I'd like the search bar to start out hidden - really hidden, not merely scrolled away as in this solution. Then I'd like to present the search UI when the user presses a button, and hide it again (really hide it) after the user selects one of the items found in the search.

这是几乎可行的代码:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.searchDisplayController.searchBar.prompt = @"Add an item";
    self.searchDisplayController.searchBar.placeholder = @"Type the item name";

    // i do this to trigger the formatting logic below
    [self.searchDisplayController setActive:YES animated:NO];
    [self.searchDisplayController setActive:NO animated:NO];
    // rest of my view will appear
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {

    NSTimeInterval duration = (self.isViewLoaded && self.view.window)? 0.3 : 0.0;
    __block CGFloat searchBarHeight = controller.searchBar.frame.size.height;

    [UIView animateWithDuration:duration animations:^{
        self.tableView.contentOffset = CGPointMake(0, searchBarHeight);
        self.tableView.contentInset = UIEdgeInsetsMake(-searchBarHeight, 0, 0, 0);  // trouble here, I think
    } completion:^(BOOL finished) {
        controller.searchBar.hidden = YES;
    }];
}

显示

- (IBAction)pressedAddButton:(id)sender {

    self.searchDisplayController.searchBar.hidden = NO;
    [self.searchDisplayController setActive:YES animated:YES];
}

我认为我已经正确设置了内容插入,但是它的行为异常:通过显示的代码,该表允许内容向上移动太远,即只有两个不太高的行,我可以向下滚动,将这两行中的大部分推至表格顶部上方...就像没有底部反弹一样.当我注释掉contentInset行时,该表允许我将内容下拉得太远,在第0行上方可能有很大的空隙,大概是搜索栏处于隐藏状态.

I think I'm properly setting the content inset, but it behaves unexpectedly: with the code as shown, the table allows the content move up too far, i.e. with only two not-very-tall rows, I'm able to scroll down, pushing most of those two rows above the top of the table...like there's no bottom bounce. When I comment out the contentInset line, the table lets me pull the content down too far, leaving a big gap above row 0, presumably where the search bar is sitting hidden.

如果有人可以提供帮助,那是非常义务的.

Much obliged if anyone can help.

推荐答案

向可能有此问题的其他人(似乎没有人)-

To anyone else who might have this problem (which appears to be nobody) -

我能找到的唯一方法是放弃UITableViewController,转而使用带有uiview的UIViewController,其子视图是表视图和搜索栏.

The only way forward I could find was to abandon the UITableViewController in favor of a UIViewController with a uiview whose children are the table view and search bar.

我按上述方式管理布局:

I manage the layout as above:

- (void)setSearchHidden:(BOOL)hidden animated:(BOOL)animated {

    UISearchBar *searchBar = self.searchDisplayController.searchBar;
    CGFloat searchBarHeight = searchBar.frame.size.height;

    CGFloat offset = (hidden)? -searchBarHeight : searchBarHeight;
    NSTimeInterval duration = (animated)? 0.3 : 0.0;

    [UIView animateWithDuration:duration animations:^{
        searchBar.frame = CGRectOffset(searchBar.frame, 0.0, offset);
        self.tableView.frame = UIEdgeInsetsInsetRect(self.tableView.frame, UIEdgeInsetsMake(offset, 0, 0, 0));
    } completion:^(BOOL finished) {if (!hidden) [searchBar becomeFirstResponder];}];
}

除了在add函数开始和结束时会被调用的方法之外.

Except in a method that gets called when the add function starts and ends.

(大约每年两次,我得出的结论是UITableViewController麻烦多于其应有的价值.然后,以大约相同的频率,我忘了学到了这一点.)

(About twice a year, I come to the conclusion that UITableViewController is more trouble than it's worth. Then, at approximately the same frequency, I forget I learned that).

这篇关于隐藏UITableView搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆