SoFunction
Updated on 2025-04-12

Organize the basic usage of UISearchBar search bar component in iOS App development

Basic properties

Copy the codeThe code is as follows:

@UISearchBar search = [[UISearchBar alloc]initWithFrame:CGRectMake(0,44,320,120)];

pragma mark - basic settings

Copy the codeThe code is as follows:

//Control style default -- 0 white, 1 is black style

/*
UIBarStyleDefault          = 0,
UIBarStyleBlack            = 1,
=UIBarStyleDefault;
/*

UISearchBarStyleDefault,
// currently UISearchBarStyleProminent

UISearchBarStyleProminent, // used my Mail, Messages and Contacts(provides no default background color or image but will display one if customized as The colors and pictures provided by such system are invalid, customization is valid)

     UISearchBarStyleMinimal    // used by Calendar, Notes and Music

     */

    =UISearchBarStyleDefault;

// The text displayed on the control

    =@"HMT";

// Single line of text displayed at the top, usually as a prompt line

    =@"DOTA";

// Translucent prompt text, input search content disappears

=@"Please enter the word you want to search for";

// The color of bar (with gradient effect) The search bar flashing bar and selection bar border, the cancel button and selection bar will become the set color when the cancel button and the selection bar are selected.

    = [UIColor redColor];

// Except for the search bar, it is like pasting a color sticker with hollowed out search bar, which does not affect any other color settings.

    = [UIColor whiteColor];

// Specify whether the control will have a perspective effect

    =YES;

// When setting it in what situation it is automatically capitalized

    /*

UITextAutocapitalizationTypeNone, //Unless you click on capital, it will never be capitalized

UITextAutocapitalizationTypeWords, //Distinguish by words, the first letter of each word is capitalized

UITextAutocapitalizationTypeSentences,           //Distinguish by sentences

UITextAutocapitalizationTypeAllCharacters, //All letters are all capitalized

     */

    =UITextAutocapitalizationTypeNone;

// Automatic correction style for text objects (Uh, I don't know what it is for)

    /*

     UITextAutocorrectionTypeDefault,

     UITextAutocorrectionTypeNo,

     UITextAutocorrectionTypeYes,

     */

    =UITextAutocorrectionTypeNo;

// Keyboard style (refer to the article UITableView for details (I))

    =UIKeyboardTypeNumberPad;


pragma mark - Set the button icon on the right of the search bar (UISearchBarIcon)
Copy the codeThe code is as follows:

// Whether to display a book button on the right end of the control

    =YES;

// Whether to display the cancel button (static)

    // = YES;

// Whether to display the cancel button (with animation effect)

    [search setShowsCancelButton:YES animated:YES];

// Whether to display the search result button on the right end of the control (the graph is a circle with a downward arrow in it)

    =YES;

// Whether the search result button is selected

    =YES;

// Set the search result button to display the right end of the control --- You can replace it with pictures

    [search setImage:[UIImage imageNamed:@""]forSearchBarIcon:UISearchBarIconResultsList state:UIControlStateNormal];


pragma mark - Select the bar at the bottom of the search bar
Copy the codeThe code is as follows:

// The selection bar at the bottom of the search bar, the content in the array is the title of the button

    = [NSArray arrayWithObjects:@"iOS",@"Android",@"iPhone",nil];

// Enter the interface, the index of the default selection bar button at the bottom of the search bar (that is, which selection bar appears first)

    =2;

// Control whether the selection bar at the bottom of the search bar is displayed (if it is displayed, you need to modify the search frame. If it is not displayed, 80 is enough)

    =YES;


pragma mark - Set control pictures
Copy the codeThe code is as follows:

// Set control background picture

    = [UIImage imageNamed:@""];

// Set the background picture in the lower part of the search bar

    = [UIImage imageNamed:@""];


pragma mark - protocol UISearchBarDelegate

(Not explained, it’s already obvious when it comes to the name)

Copy the codeThe code is as follows:

@Edit text

// When UISearchBar gets focus and starts editing, execute this method

(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;           // return NO to not become first responder

(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{          // called when text starts editing

[searchBar setShowsCancelButton:YES animated:YES];   //  Animation display Cancel button

}

(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;       // return NO to not resign first responder

(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;            // called when text ends editing

(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{   // called when text changes (including clear)

@ When the search content changes, execute this method. Very useful, real-time search

}


Copy the codeThe code is as follows:

(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)textNS_AVAILABLE_IOS(3_0);                 // called before text changes
@button click

(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;     // called when keyboard search button pressed

(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;        // called when bookmark button pressed

(void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{           // called when cancel button pressed

[searchBar setShowsCancelButton:NO animated:NO];    // Cancel button recycling

[searchBar resignFirstResponder];                                                                                                                       �

}

(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBarNS_AVAILABLE_IOS(3_2);// called when search results button pressed

(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScopeNS_AVAILABLE_IOS(3_0);

Data brush selection category: NSPredicate

Copy the codeThe code is as follows:

@Hypothesis: NSArray array = [[NSArray alloc]initWithObjects:@"luna",@"moon",@"",@"lion",@"coco", nil];

// The processing of data mainly occurs in this method

(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

// Method 1: ([c] is case-sensitive [d] is not pronunciation symbol, that is, there is no accent symbol [cd] is neither case-sensitive nor pronunciation symbol.)

    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@",searchText];

// The fast traversal provided by the array, the returned type is NSArray

   NSLog(@"%@",[ _array filteredArrayUsingPredicate:predicate]);

// Method 2:

    for (int i = 0; i count]; i++) {

        if ([predicate evaluateWithObject:[ _array objectAtIndex:i]]) {

            NSLog(@"%@",[arrayobjectAtIndex:i]);

        }

    }

}