【发布时间】:2012-03-21 05:06:57
【问题描述】:
我有一个 UITableViewController,它以标准方式设置了 UISearchDisplayController(在 tableView 内使用搜索栏)。我希望搜索栏一开始是隐藏的 - 真正隐藏,而不仅仅是滚动离开 as in this solution。然后我想在用户按下按钮时呈现搜索 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;
}];
}
展示
- (IBAction)pressedAddButton:(id)sender {
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController setActive:YES animated:YES];
}
我认为我正确设置了内容插入,但它的行为出乎意料:使用所示代码,表格允许内容向上移动太多,即只有两个不太高的行,我可以向下滚动,将这两行中的大部分推到表格顶部上方……就像没有底部反弹一样。当我注释掉 contentInset 行时,表格让我将内容拉得太远,在第 0 行上方留下一个很大的空隙,大概是搜索栏所在的位置。
如果有人能提供帮助,我们将不胜感激。
【问题讨论】:
-
搜索栏隐藏后表格有2行,每行70px高,表格高度=367,内容offset.y=0,内容inset.top=-75,inset.bottom =0,一切如预期。是什么让我卡在这里是导致“底部反弹”的原因?当内容只有 140px 高并且视图是 367 高时,滚动视图不应该让我滚动,但是为什么我可以滚动第一行呢?
标签: iphone ios uitableview uisearchbar uisearchdisplaycontroller