3

我有一个UITableViewController与UISearchDisplayController安装程序的标准方式(与tableView内的搜索栏)。我想要搜索栏开始隐藏 - 真的隐藏起来,不仅仅是滚动as in this solution。然后,我想在用户按下按钮时显示搜索UI,并在用户选择搜索中找到的其中一个项目后再次隐藏(实际上隐藏它)。隐藏UITableView搜索栏

下面是该几乎工作代码:

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

非常感谢,如果任何人都可以提供帮助。

+0

隐藏搜索栏后,表格有2行,每个70px高度,表格高度= 367,内容offset.y = 0,内容inset.top = -75,inset.bottom = 0,全部为预期。什么让我卡在这里是什么导致“底部反弹”?滚动视图不应允许我滚动内容只有140px高,视图367高,但为什么我可以滚动第一行? – danh 2012-03-21 14:53:03

回答

11

为了别人谁可能有这个问题(这似乎是一个人) -

前进的唯一道路我可以找到的是放弃UITableViewController赞成UIViewController uiview的子级是表视图和搜索栏。

我管理的布局如上:

- (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];}]; 
} 

除的方法被调用时附加功能开始和结束。 (大约每年两次,我得出UITableViewController比它的价值更麻烦的结论,然后,以大致相同的频率,我忘记了我学到了这一点)。如果有足够的条目来完全填满屏幕

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

这仅适用于:

+0

这太棒了!我刚刚添加searchbar.alpha = 0;给动画... – Hackmodford 2012-11-01 19:53:33

+1

+1为“我忘了我学到了”:D – bennythemink 2014-02-10 03:07:55

0

与 “阿尔法” 的方法去:

[controller.searchBar setAlpha:0.0]; 

[controller.searchBar setAlpha:1.0]; 
+0

谢谢你的想法,但没有区别。 – danh 2012-03-21 13:52:01

0

如果您的表有足够的条目,你可以做以下。当然,如果你只有一个或两个条目,你可能并不在乎隐藏搜索栏。