3
votes

Update 03/25/14

It looks like the lack of animation was a bug in iOS 7.0, and has been fixed in iOS 7.1.

Original Question

Normally, when you tap inside a search bar in iOS 7, it has a nice little animation where the magnifying glass and "Search" text slides from the middle of the search bar to the left side. However, if I put the search bar in the navigation bar instead of within a table view, it no longer animates. Instead, the magnifying glass and "Search" text jumps instantly to the left side of the search bar.

You can see this really easily using Apple's UISearchBar sample project. Open up the storyboard. Add a boolean runtime attribute displaysSearchBarInNavigationBar on Search Display Controller, and check the checkbox it creates. Then, move the Search Bar up a level in the tree, so it is outside of the Table View. Run the project, and the search bar should now be in the navigation bar. Note that when you tap in the search bar, it doesn't animate.

Has anyone successfully put a UISearchBar in the navigation bar in iOS 7, and had it animate properly?

To be clear, my problem is that there is no animation of the search bar between the first and second screenshots below, when the user taps on the search bar.

beforeafter

1
try with this link github.com/iDevAndroid/TableSearch – codercat

1 Answers

5
votes

In iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's displaysSearchBarInNavigationBar = YES for to get this working easily.

Apple Inc Doc: Starting in iOS 7.0, you can use a search display controller with a navigation bar (an instance of the UINavigationBar class) by configuring the search display controller’s displaysSearchBarInNavigationBar and navigationItem properties.

your expected answer

Remove the search bar from the table view. Using displaysSearchBarInNavigationBar means that UISearchDisplayController will take care of placing the search bar in the hierarchy for you.

- (void)viewDidLoad
{
    self.searchDisplayController.displaysSearchBarInNavigationBar=YES;   
}

enter image description here

This was not animation first time tap but the animation works good in cancel moments:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
[searchBar setShowsCancelButton:YES animated:YES]; 

} 


-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
{ 
[searchBar setShowsCancelButton:NO animated:YES]; 
}