隐藏UITableView搜索栏

dan*_*anh 3 iphone uitableview uisearchbar uisearchdisplaycontroller ios

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

这是几乎可以工作的代码:

- (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;
    }];
}
Run Code Online (Sandbox Code Playgroud)

显示

- (IBAction)pressedAddButton:(id)sender {

    self.searchDisplayController.searchBar.hidden = NO;
    [self.searchDisplayController setActive:YES animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

我想我正确地设置内容插图,但表现异常,请:与如图所示的代码,该表允许内容只有两个不是非常高的行向上移动太远,即,我能够向下滚动,把这两排大部分推到桌子顶部......好像没有底部反弹.当我注释掉contentInset行时,该表允许我将内容拉得太远,在第0行上方留下一个很大的间隙,大概是搜索栏隐藏的位置.

如果有人可以提供帮助,那就很有必要.

dan*_*anh 11

对于可能有这个问题的任何人(似乎没有人) -

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

我按上面的方式管理布局:

- (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];}];
}
Run Code Online (Sandbox Code Playgroud)

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

(大约每年两次,我得出结论,UITableViewController比它的价值更麻烦.然后,在大致相同的频率,我忘了我学到了).