IOS

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


Basic attributes

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

pragma mark - basic Settings

// Control style   The default --0 white ,1 It's black
/*
UIBarStyleDefault          = 0,
UIBarStyleBlack            = 1,
search.barStyle =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 such The colors and pictures provided by the system are invalid , Self-designed and effective )
     UISearchBarStyleMinimal    // used by Calendar, Notes and Music
     */
    search.searchBarStyle =UISearchBarStyleDefault;
    //  Controls the text displayed above
    search.text =@"HMT";
    //  A single line of text displayed at the top, usually as 1 A presenting bank
    search.prompt =@"DOTA";
    //  Translucent prompt text, input search content disappeared
    search.placeholder =@" Please enter the word to search for ";
    // bar The color of the ( It has a gradient effect ) Search bar flash bar and selection bar border , Both the cancel button and the selection bar change to the set color when selected
    search.tintColor = [UIColor redColor];
    //  Except for the search box , As posted 1 Zhang has hollowed out the color map of the search bar , Does not affect the color of any other Settings
    search.barTintColor = [UIColor whiteColor];
    //  Specifies whether the control will have a perspective effect
    search.translucent =YES;
    //  Set auto capitalization in what case
    /*
     UITextAutocapitalizationTypeNone,             // Unless you click uppercase yourself , Otherwise never capitalize
     UITextAutocapitalizationTypeWords,            // Distinguish by word , Each word begins with a capital letter
     UITextAutocapitalizationTypeSentences,        // Distinguish by sentence
     UITextAutocapitalizationTypeAllCharacters,    // All letters are capitalized
     */
    search.autocapitalizationType =UITextAutocapitalizationTypeNone;
    //  Automatic style correction for text objects ( The forehead , I don't know what's the use )
    /*
     UITextAutocorrectionTypeDefault,
     UITextAutocorrectionTypeNo,
     UITextAutocorrectionTypeYes,
     */
    search.autocorrectionType =UITextAutocorrectionTypeNo;
    //  Keyboard style ( Refer to the article for details UITableView Break down (1))
    search.keyboardType =UIKeyboardTypeNumberPad;

pragma mark - set the button icon to the right of the search bar (UISearchBarIcon)

    //  Whether to display on the right side of the control 1 Book buttons
    search.showsBookmarkButton =YES;
    //  Whether or not shown cancel button ( static )
    //search.showsCancelButton = YES;
    //  Whether or not shown cancel button ( It's animated )
    [search setShowsCancelButton:YES animated:YES];
    //  Whether to display the search results button on the right side of the control ( Graphics is 1 It's in a circle 1 A down arrow )
    search.showsSearchResultsButton =YES;
    //  Whether the search results button is selected
    search.showsSearchResultsButton =YES;
    //  Set the right end of the control to display the search results button  ---  You can replace it with a picture
    [search setImage:[UIImage imageNamed:@"qiyi.png"]forSearchBarIcon:UISearchBarIconResultsList state:UIControlStateNormal];

pragma mark - search bar bottom selection bar

    //  The selection bar at the bottom of the search bar, and the content in the array is the title of the button
    search.scopeButtonTitles = [NSArray arrayWithObjects:@"iOS",@"Android",@"iPhone",nil];
    //  Enter interface , The index of the default selection bar button at the bottom of the search bar ( That is the first 1 Appears in which selection bar )
    search.selectedScopeButtonIndex =2;
    //  Controls whether the selection bar below the search bar is displayed ( According to it , To modify the search the frame, If you don't show it 80 It is enough )
    search.showsScopeBar =YES;

pragma mark - sets control image

    //  Sets control background image
    search.backgroundImage = [UIImage imageNamed:@"qiyi.png"];
    //  Set the background image below the search bar
    search.scopeBarBackgroundImage = [UIImage imageNamed:@"qiyi.png"];

pragma mark - protocol UISearchBarDelegate

(no explanation, look at the name, it’s obvious)

@ Edit text
 // UISearchBar Execute this method when you get focus and start editing
(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 shows the 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)
   @  This method is executed when the search content changes. Very useful, you can do real-time search
}
(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)textNS_AVAILABLE_IOS(3_0);                 // called before text changes
@ Click on the button
(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 recall
    [searchBar resignFirstResponder];                                //  Cancel the first 1 Response values , Keyboard recycling , End of the search
}
(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 class :NSPredicate

@ Assuming that : NSArray array = [[NSArray alloc]initWithObjects:@"luna",@"moon",@"",@"lion",@"coco", nil];
//  The processing of the data occurs primarily in this method
(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    //  methods 1:([c] Case insensitive [d] The absence of a phonetic symbol is the absence of an accent [cd] It is neither case sensitive nor phonemic. )
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@",searchText];
   //   Arrays provide quick traversal , The return type is NSArray
   NSLog(@"%@",[ _array filteredArrayUsingPredicate:predicate]);
    //  methods 2:
    for (int i = 0; i count]; i++) {
        if ([predicate evaluateWithObject:[ _array objectAtIndex:i]]) {
            NSLog(@"%@",[arrayobjectAtIndex:i]);
        }
    }
}