UITableViewController - 下拉 table 视图以显示新的 UIView

UITableViewController - Drop down table view to show a new UIView

我正在尝试设计一个 UITableViewController,这样当用户点击导航栏上的 "Search" 按钮时,table 视图会下降,并且 UIView 从导航栏中下降。然后,当用户点击 UIView 之外的任何地方时,UIView 应该缩回并且 table 视图应该 return 到它的原始位置。

目前,我有以下方法作为 "Search" 按钮的选择器。注意 - self.searchBar 是一个自定义的 UIView 子类。

是否有 cleaner/better 方法来完成此操作?此外,我不确定在用户点击搜索菜单后如何摆脱视图......我猜我应该打电话,[self.searchBar removeFromSuperview];但不确定将该行放在哪个委托方法中。

谢谢!

- (void)_showSearchMenu
{
  CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * .25);
  frame.origin.y = CGRectGetMaxY(self.navigationController.navigationBar.frame) - frame.size.height;
  self.searchBar.frame = frame;

  [self.navigationController.navigationBar.superview insertSubview:self.searchBar belowSubview:self.navigationController.navigationBar];

  [UIView animateWithDuration:0.5 animations:^{
    CGRect frame = self.searchBar.frame;
    frame.origin.y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
    self.searchBar.frame = frame;
    self.tableView.contentOffset = CGPointMake(0, -250);
  }];
}

更清楚地说,我正在尝试实现类似于在此处的 HotelTonight 应用程序中看到的效果(第二个屏幕显示当您点击右上栏按钮时发生的情况)

  • 在 superview 上添加 Tapgesture
  • 在 TapGesture 动作中检查 searchBar 视图是否可见
  • 如果 Visible 通过设置高度为零的新框架隐藏 DropDown 视图
  • 您可以通过编程方式或从 Interface Builder 添加点击手势,您可以使用其委托方法 "shouldReceiveTouch" 或任何其他自定义操作。 Gesture Implementation

这是我认为最好的方法,使用这些代表:

(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

如何:

  1. 创建默认值为NO
  2. BOOL isOpen
  3. 当您单击“搜索”按钮时,执行此操作:

    (void) searchButtonTouch:(id)sender { 
        isOpen = (isOpen) ? YES : NO;
        isOpen = !isOpen; 
        [self.urTableView reloadData];
    }
    
  4. 现在在您的代表中:

    (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return (isOpen) ? 170.0f : 0.0f;
    }
    
    (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
        CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
        UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, height)];
        vw.backgroundColor = [UIColor yellowColor];
    
        // add other controls here in your UIView
        // or
        // just add a UIView at top of your UITableView
        // then add other controls to it (if ur using storyboard)
    
        return vw;
    }