Hide UITableView search bar

I have a UITableViewController with the UISearchDisplayController set in the standard way (with a search bar inside the tableView). I would like the search bar to start hiding - really hidden, not just scrolling like in this solution . I would then like to expose the search UI when the user clicks on a button, and hides it again (really hides) after the user selects one of the items found in the search.

Here is almost working code for that:

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

      

to show

- (IBAction)pressedAddButton:(id)sender {

    self.searchDisplayController.searchBar.hidden = NO;
    [self.searchDisplayController setActive:YES animated:YES];
}

      

I think I have set up the nested content correctly, but it behaves unpredictably: with the code as shown, the table allows the content to be moved too far, i.e. with only two not very tall rows, I can to scroll down by clicking most of those two rows over the top of the table ... as there is no bounce bottom. When I comment out the contentInset line, the table allows me to pull the content too far, leaving a large space above line 0, presumably where the search bar is hidden.

It is very important if someone can help.

+3


source to share


3 answers


Anyone who might have this problem (which seems like no one else) -

The only way I could find was to ditch the UITableViewController in favor of a UIViewController with a uiview whose children are table and search bar.

I manage the layout as above:



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

      

Except for the method that is called when the add function starts and ends.

(About twice a year, I came to the conclusion that the UITableViewController is more problem than it was worth. Then, at about the same frequency, I forgot I found out about it).

+11


source


Go with the alpha approach:

[controller.searchBar setAlpha:0.0]; 

      



or

[controller.searchBar setAlpha:1.0]; 

      

0


source


If you have enough records in the table, you can do the following:

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

      

This only works if there are enough entries to fill the screen completely. Of course, if you only have one or two entries, you don't have to hide the search bar.

0


source







All Articles