隐藏UITableViewsearch栏

我有一个UITableViewController与UISearchDisplayController设置标准的方式(在tableView内的search栏)。 我想要search栏开始隐藏 – 真正隐藏的,不仅仅是在这个解决scheme中滚动。 然后,我想在用户按下button时显示searchUI,并在用户selectsearch中find的项目之后再次隐藏(真的隐藏)。

这是几乎工作的代码:

- (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行之上留下了一个很大的差距,大概是search栏隐藏起来的地方。

如果有人可以帮忙,非常感激。

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

我唯一能find的方法是放弃UITableViewController,转而使用uiview的UIViewController,其子表是视图和search栏。

我如上pipe理布局:

 - (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函数开始和结束时被调用的方法外。

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

去“阿尔法”的方法:

 [controller.searchBar setAlpha:0.0]; 

要么

 [controller.searchBar setAlpha:1.0]; 

如果您的表中有足够的条目,则可以执行以下操作:

 [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; 

这只有在有足够的条目来完全填满屏幕时才有效。 当然,如果你只有一个或两个条目,你可能并不在乎隐藏search栏。