Reputation: 963
How can I use a textfield rather than a search bar with the UISearchDisplay controller?
I want to completely customize the search bar by getting rid of the magnifying glass icon and customizing the background. Also stopping it from resizing and bringing up the 'cancel' button. I see some people using hacky ways to do this by editing parts of the search bar api that weren't supposed to be edited. So it seems the more accepted way to do customization on this level would be to use a UITextfield instead of a UISearchBar. But there doesn't seem to be ANY info on the web about doing this!
If I use a textfield, what methods do I need to call when text changes to make the UISearchDisplayController work?
Upvotes: 1
Views: 2824
Reputation: 3191
The trick here is to rewrite the UISearchDisplayController. It really only does 3 things.
So start by registering your UIViewController as a delegate for the UITextField and..
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//here is where you open the search.
//animate your textfield to y = 0.
//I usually make the search tableview and the cover a separate view,
//so I add them to my view here.
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *searchText = [[NSString alloc] initWithString:[textField.text stringByReplacingCharactersInRange:range withString:string]];
//display your search results in your table here.
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
//hide all of your search stuff
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
Upvotes: 5