简体   繁体   English

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

[英]UITableViewController - Drop down table view to show a new UIView

I'm trying to design a UITableViewController such that when the user hits the "Search" button on the navigation bar, the table view drops down and a UIView drops down from the navigation bar. 我正在尝试设计一个UITableViewController,当用户点击导航栏上的“搜索”按钮时,表格视图会下降,UIView会从导航栏下拉。 Then, when the user taps anywhere outside of the UIView, the UIView should retract and the table view should return to its original position. 然后,当用户点击UIView之外的任何地方时,UIView应该缩回并且表视图应该返回到其原始位置。

Currently, I have the following method that is the selector for the "Search" button. 目前,我有以下方法,即“搜索”按钮的选择器。 Note - self.searchBar is a custom UIView subclass. 注意 - self.searchBar是一个自定义的UIView子类。

Is there a cleaner/better way to accomplish this? 是否有更清洁/更好的方法来实现这一目标? Also I'm not sure how to get rid of the view after the user taps out of the search menu... I'm guessing I should call, [self.searchBar removeFromSuperview]; 在用户点击搜索菜单后,我也不确定如何摆脱视图......我猜我应该打电话,[self.searchBar removeFromSuperview]; but not sure in which delegate method to put that line. 但不确定将该行放在哪个委托方法中。

Thanks! 谢谢!

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

To be more clear, I'm trying to achieve something similar to the effect seen in the HotelTonight app here (the second screen shows what happens when you hit the top right bar button) 为了更清楚,我试图在这里尝试类似于HotelTonight应用程序中看到的效果(第二个屏幕显示当您点击右上方的按钮时会发生什么)

在此输入图像描述

在此输入图像描述

This is I think the best approach for that, use these delegates: 这是我认为最好的方法,使用这些代表:

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

How: 怎么样:

  1. Create a BOOL isOpen with a default value of NO 创建一个BOOL isOpen ,默认值为NO
  2. When you click the Search Button, implement this: 单击“搜索”按钮时,请执行以下操作:

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

     (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; } 
  • Add Tapgesture on superview 在superview上添加Tapgesture
  • In TapGesture Action check in if is searchBar view visible 在TapGesture Action中,检查searchBar视图是否可见
  • If Visible hide DropDown view by setting new frame with height zero 如果Visible通过设置高度为零的新框架隐藏DropDown视图
  • You can Add Tap Gesture Programmatically or from Interface Builder , You can use its delegate method "shouldReceiveTouch" or any other custom action. 您可以通过编程方式或从Interface Builder添加Tap Gesture,您可以使用其委托方法“shouldReceiveTouch”或任何其他自定义操作。 Gesture Implementation 手势实施

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM