views:

2708

answers:

2

I have a subclass of UITableViewController. I have code that can add/remove a UISearchBar to/from the tableHeaderView of my tableView. Here's the code I have to perform these tasks:

self.tableView.tableHeaderView = uiSearchBar; //Make the search bar appear
self.tableView.tableHeaderView = nil; //Make the search bar disappear

The problem is that I want the adding/removing of the UISearchBar to be animated; slide into view from the top when I add it then slide upwards and out of view when I remove it instead of just appearing and disappearing. Any suggestions?

Thanks.

A: 

Try having your table view scroll—animated—so that only the row below the search bar is visible, then remove the search bar and scroll to the same row without animation.

Noah Witherspoon
+4  A: 

I was finally able to figure this out. Turned out to be pretty simple. Instead of adding the UISearchBar to the table header, I insert it into the first cell of the table with animation UITableViewRowAnimationTop and remove it using the same method. This results in the bar sliding in and out from the top. Here is the code that makes the bar appear:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.baseUiTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.baseUiTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

And here is the code that removes the search bar:

[uiSearchBar resignFirstResponder];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.baseUiTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
Peter Parker