【问题标题】:UITableViewController - Drop down table view to show a new UIViewUITableViewController - 下拉表格视图以显示新的 UIView
【发布时间】:2015-08-11 05:10:44
【问题描述】:

我正在尝试设计一个 UITableViewController,这样当用户点击导航栏上的“搜索”按钮时,表格视图会下降,而 UIView 会从导航栏下降。然后,当用户点击 UIView 之外的任何位置时,UIView 应该缩回并且表格视图应该返回到它的原始位置。

目前,我有以下方法是“搜索”按钮的选择器。注意 - self.searchBar 是一个自定义 UIView 子类。

是否有更清洁/更好的方法来完成此任务?此外,我不确定在用户点击搜索菜单后如何摆脱视图......我猜我应该调用,[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 应用程序中看到的效果(第二个屏幕显示当您点击右上角的栏按钮时会发生什么)

【问题讨论】:

    标签: ios objective-c uitableview uiview uiviewcontroller


    【解决方案1】:

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

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

    如何:

    1. 创建一个默认值为NOBOOL isOpen
    2. 当你点击搜索按钮时,实现这个:

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

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

    【讨论】:

      【解决方案2】:
      • 在超级视图上添加 Tapgesture
      • 在 TapGesture 操作中检查 searchBar 视图是否可见
      • If Visible 通过设置高度为零的新框架来隐藏 DropDown 视图
      • 您可以通过编程方式或从 Interface Builder 添加 Tap Gesture,您可以使用其委托方法“shouldReceiveTouch”或任何其他自定义操作。 Gesture Implementation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-31
        • 1970-01-01
        相关资源
        最近更新 更多