Posts Tagged ‘customization’

There are no accessors to the buttons and subviews of the UISearchBar public available by default, but we can access them thought the subviews method of the searchBar.

But why this is cool? Imagine you design a view and want a search bar on it. In order to go back from the search, or you need navigation bar with a back button, or you will need to create a back button for yourself in some place. But wait, the search bar can show a “cancel” button, can’t it? So you foward to use the cancel button and you realize that it comes disable until the user write something in the textfield. And we don’t want our users to need to type a letter in order to leave the search page. So with this trick, it’s pretty simple to enable the cancel button, even when the user typed nothing.

Put this code in the viewDidLoad method, or any place you prefer.

for (UIView *possibleButton in searchBar.subviews) {

if ([possibleButton isKindOfClass:[UIButton class]]){

UIButton *cancelButton = (UIButton*)possibleButton;

cancelButton.enabled = YES;




Now you are done. Your cancel button will be available to the user even if he didn’t type any character.

Then yon can implement this method:

– (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {

[self dismissModalViewControllerAnimated:YES];


and dismiss the view controller or anything you want. You can even change the text of the button with the method setTitle:ForState: of the button.

[cancelButton setTitle:@”Back” forState:UIControlStateNormal];


Read Full Post »