Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar animation hiding button

I currently have a UISearchBar (attached to a UISearchDisplayController), but I reduced the width of the search bar so I could display a custom button to its right when the search bar is not selected. The button is used to access other views.

However, when I select the search bar and then press cancel (or even perform a search) and return to the normal view, where the search bar should be displayed with my custom button, the search bar animates and takes up all the room for the button and the is not displayed. Essentially, the search bar takes up all the width of the screen when I only want it to take up a part of it.

Is there any way to prevent the search bar from animating to the whole width of the screen?

This is how I defined the CGRect of the search bar:

self = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 250.0f, 45.0f)]
like image 354
David Carvalho Avatar asked Jun 09 '10 13:06

David Carvalho


2 Answers

In the end, the answer to this was simpler than I thought. For some reason the iPhone SDK fills up the width of the frame of the search bar, so I have to constantly reset it to the size I want every time the user presses the cancel button and the searchBarCancelButtonClicked() event is triggered. A simple [searchBar setFrame: frame] solved it. :)

Peter, the problem in the post you linked was slightly different, I had already been able to resize the search bar to what I wanted, what I needed was to stop it going back to full width. Thanks for the link though.

like image 95
David Carvalho Avatar answered Oct 20 '22 00:10

David Carvalho


This SO post might help. It does sound like you need to subclass UISearchBar to stop it doing its normal drawing.

UPDATE I wanted to use a UISearchBar in a UINavigationBar, and have it resize to fill the bar when touched - just like the behaviour in the Safari app. The only way I could do this was to use UISearchBar directly. Your issue is slightly different, but you could try getting the UI behaviour you want by using just the search bar to start with; then wire-up the logic for searching afterwards; i.e. don't rely on UISearchDisplayController?

like image 44
petert Avatar answered Oct 19 '22 23:10

petert