View Full Version : UIBarButtonItem animations

Jan 19, 2011, 11:24 AM
This is what I have in my navigationItem: back button, UISearchBar (as the titleView) which is a part of my subclassed searchDisplayController, and map button in the rightBarItem slot.

When a user clicks into the UISearchBar, the back button and mapbutton animate away, the searchBar fills up the gap left by the back button disappearing, and the cancel button appears. This is working fine.
I've done this with the searchBarTextDidBeginEditing method:
self.navigationItem.hidesBackButton = YES;
self.navigationItem.rightBarButtonItem = nil;

However, I can't get the reverse animation just right, when the user leaves the searchBar. I'm allocating a new UIBarButtonItem and putting it into the navigationItem.rightBarButtonItem, and I'm setting self.navigationItem.hidesBackButton = NO in the searchBarTextDidEndEditing delegate method. What happens is that the buttons appear very sudden, the searchBar expands to the full width of the screen (which I want to prevent from happening), and then shrinks again to where it ought to be.

Does anybody have any suggestions to get the animations to behave well?


Jan 20, 2011, 12:12 AM
So there's been a bit of improvement. I tried [self.navigationItem.titleView sizeToFit] to see what sort of effect it might have, and now search bar plays nicely with the back button, but not the right button!

Here is my code:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
self.navigationItem.hidesBackButton = YES;
self.navigationItem.rightBarButtonItem = nil;

- (void)searchBarTextDidEndEditing:(UISearch *)searchBar {
self.navigationItem.hidesBackButton = NO;
UIBarButtonItem *mapButton = [[UIBarButtonItem alloc] initWithTitle:@"Map" style: UIBarButtonItemStyleBordered target:self action:@selector(mapView:)];
self.navigationItem.rightBarButtonItem = mapButton;
[mapButton release];
[self.navigationItem.titleView sizeToFit];

So to recap...

Starting State: searchBar takes up the left and middle, cancel button on the right
Transition State: back button takes up left, searchBar takes up the middle and right, mapButton takes up the right as well
End State: searchBar is in the middle

Any help would be greatly appreciated :)