user3298872
user3298872

Reputation: 231

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. Then, when the user taps anywhere outside of the UIView, the UIView should retract and the table view should return to its original position.

Currently, I have the following method that is the selector for the "Search" button. Note - self.searchBar is a custom UIView subclass.

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

enter image description here

enter image description here

Upvotes: 3

Views: 942

Answers (2)

Pepeng Hapon
Pepeng Hapon

Reputation: 367

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

How:

  1. Create a BOOL isOpen with a default value of 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;
    }
    

Upvotes: 2

abrar ul haq
abrar ul haq

Reputation: 404

  • Add Tapgesture on superview
  • In TapGesture Action check in if is searchBar view visible
  • If Visible hide DropDown view by setting new frame with height zero
  • You can Add Tap Gesture Programmatically or from Interface Builder , You can use its delegate method "shouldReceiveTouch" or any other custom action. Gesture Implementation

Upvotes: 0

Related Questions