iOS Development--ui Advanced (19) Introduction to Uisearchbar controls

Source: Internet
Author: User

Recently used the search function. Summarize

Search, no doubt you can use the Uisearchbar control!

Let's look at the Uisearchbar control first!


The Uisearchbar control is a dedicated control to complete the search function for you. It integrates many of your unexpected features and features!

First of all, it is also to popularize the Uisearchbar control API related properties and methods!

Uisearchbar Property Related

_searchbar = [[Uisearchbar alloc] Initwithframe:cgrectzero];//Initialize, do not explain[Self.searchbar Setplaceholder:@"Search"];//placeholder for search box[Self.searchbar setprompt:@"Prompt"];//tip text at the top, equivalent to the title of the control[Self.searchbar Setbarstyle:uibarmetricsdefault];//Search box Style[Self.searchbar Settintcolor:[uicolor Blackcolor];//the color of the search box, Barstyle will expire when this property is set[Self.searchbar Settranslucent:yes];//set whether transparent[Self.searchbar setbackgroundimage:[uiimage imagenamed:@"IMAGE0"]];//set a background picture[Self.searchbar setsearchfieldbackgroundimage:[uiimage imagenamed:@"Image3"] Forstate:uicontrolstatenormal];//set the background of a text box in the search box[Self.searchbar setsearchfieldbackgroundimage:[uiimage imagenamed:@"IMAGE0"] forstate:uicontrolstatehighlighted]; [Self.searchbar Setsearchfieldbackgroundpositionadjustment:uioffsetmake ( -, -)];//sets the offset of the background of the text box in the search box[Self.searchbar Setsearchresultsbuttonselected:no];//sets whether the search Results button is selected[Self.searchbar Setshowssearchresultsbutton:yes];//whether to display the search results button[Self.searchbar Setsearchtextpositionadjustment:uioffsetmake ( -,0)];//set the text offset of the text box in the search box[Self.searchbar setinputaccessoryview:_btnhide];//provide a shaded view[Self.searchbar setkeyboardtype:uikeyboardtypeemailaddress];//Set Keyboard style//set the column bar below the search box[Self.searchbar Setshowsscopebar:yes];//whether to display the columns bar[Self.searchbar Setscopebuttontitles:[nsarray arraywithobjects:@"Singer",@"Song",@"Album", Nil]];//column bar, column[Self.searchbar setscopebarbackgroundimage:[uiimage imagenamed:@"Image3"]];//background color of the column bar[Self.searchbar Setselectedscopebuttonindex:1];//Subscript for the button selected by default in the column bar[Self.searchbar Setshowsbookmarkbutton:yes];//do you want to show the book icon on the right?[Self.searchbar Setshowscancelbutton:yes];//whether to show the Cancel button[Self.searchbar Setshowscancelbutton:yes Animated:yes]; //whether to provide automatic correction function (this method is generally not used)[Self.searchbar Setspellcheckingtype:uitextspellcheckingtypeyes];//set the type of automatic check[Self.searchbar Setautocorrectiontype:uitextautocorrectiontypedefault];//whether to provide automatic correction function, generally set to UitextautocorrectiontypedefaultSelf.searchbar.Delegate= self;//Set up proxy[Self.searchbar SizeToFit]; Mytableview.contentinset= Uiedgeinsetsmake (Cgrectgetheight (self.searchBar.bounds),0,0,0);        [Self.view Addsubview:mytableview]; [Mytableview AddSubview:self.searchBar];

A bit more property, need to do more than two times

Most of the parts are defined to look something! Understand the various properties, will be able to meet you to design the appearance you want to effect!!

Then, to explain, I personally feel the more interesting and important attributes!


[Email protected] (Nonatomic, ReadWrite, retain) UIView *inputaccessoryview; properties

For example:

[Self.searchbar setinputaccessoryview:your_view];//provides a masking view

When in the Uisearchbar Focus state (when the input box is about to be entered), there is a shaded view.

You look at the Phone book Search feature on your iphone. The cover view is a semi-transparent black view.

Check out the API, iOS 6.0 and later, new to join!
Then it means that the system before IOS 6.0 is incompatible. So how can we achieve this similar effect?
In fact, it's simple: still set a button, initially, the UIButton control has a transparency set of 0, and when the control gets focus, set the transparency to 1.
Tip: If you want to set this property, it's best to define a UIButton control so that when you click on the overlay, you can use the button event,
Settings: [Self.searchbar Resignfirstresponder]; Let the search box give up the first focus. (The iphone phone book is doing the same, feeling very humane).

Confusion: There is a small problem: when I let Uisearchbar display the Cancel button, when I let Uisearchbar lose focus, my Cancel button also cannot click. What a loser.
Look at the iphone phone thin uisearchbar, unexpectedly can also, looking for a long time, do not know what is going on, probably Apple started playing private API again.
Workaround: Very violent, but very useful! On the Uisearchbar the original Cancel button on the location of a UIButton, set to the same. Oh. OK.

Similar to the following:

//cover Layer_btnaccessoryview=[[uibutton Alloc] Initwithframe:cgrectmake (0, -, Bounds_width, Bounds_height)]; [_btnaccessoryview Setbackgroundcolor:[uicolor Blackcolor]; [_btnaccessoryview Setalpha:0.0f]; [_btnaccessoryview addtarget:self Action: @selector (clickcontrolaction:) forControlEvents: UIControlEventTouchUpInside]; [Self.view Addsubview:_btnaccessoryview];//Mask Layer (button)-click Handle event- (void) Clickcontrolaction: (ID) sender{NSLog (@"Handletaps"); [Self Controlaccessoryview:0];}//control the opacity of the mask layer- (void) Controlaccessoryview: (float) alphavalue{[UIView animatewithduration:0.2animations:^{        //Animation Code[Self.btnaccessoryview Setalpha:alphavalue]; }completion:^(BOOL finished) {if(alphavalue<=0) {[Self.searchbar resignfirstresponder];            [Self.searchbar Setshowscancelbutton:no Animated:yes];        [Self.navigationcontroller Setnavigationbarhidden:no Animated:yes]; }            }];}

[Email protected] (nonatomic,assign) id<</b>uisearchbardelegate> delegate; properties

For example:

Self.searchBar.delegate = self;

When it comes to this attribute, the delegate is set.

The Uisearchbardelegate delegate defines a lot of protocol methods for some of the operation data of the search box!

First, close-up, list the family members of the X protocol:

@protocol uisearchbardelegate@optional-(BOOL) searchbarshouldbeginediting: (Uisearchbar *) Searchbar;- (void) Searchbartextdidbeginediting: (Uisearchbar *) Searchbar;-(BOOL) searchbarshouldendediting: (Uisearchbar *) Searchbar;- (void) Searchbartextdidendediting: (Uisearchbar *) Searchbar;- (void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) SearchText;-(BOOL) Searchbar: (Uisearchbar *) Searchbar Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text; - (void) searchbarsearchbuttonclicked: (Uisearchbar *) Searchbar;- (void) searchbarbookmarkbuttonclicked: (Uisearchbar *) Searchbar;- (void) searchbarcancelbuttonclicked: (Uisearchbar *) Searchbar;- (void) searchbarresultslistbuttonclicked: (Uisearchbar *) Searchbar;- (void) Searchbar: (Uisearchbar *) Searchbar Selectedscopebuttonindexdidchange: (Nsinteger) selectedscope; @end

This does not need to explain, look at the method name can understand!

Let's take a look at the usual method of entrusting.

#pragmaMark-uisearchbardelegate protocol//when Uisearchbar gets the focus and starts editing, the method is executed-(BOOL) searchbarshouldbeginediting: (Uisearchbar *) searchbar{[Self.searchbar Setshowscancelbutton:yes Animated:yes];    [Self.navigationcontroller Setnavigationbarhidden:yes Animated:yes]; [Self Controlaccessoryview:0.9];//shows the masking layer.     returnYES;}//the method to execute when the Cancel button is pressed- (void) searchbarcancelbuttonclicked: (Uisearchbar *) searchbar{[Self.searchbar Resignfirstresponder];    [Self.searchbar Setshowscancelbutton:no Animated:yes]; [Liveviewareatable Searchdatabysearchstring:nil];//Search TableView Data[Self.navigationcontroller setnavigationbarhidden:no Animated:yes]; [Self Controlaccessoryview:0];//hide the cover layer.     }//Keyboard, the search button is pressed, the method of execution- (void) searchbarsearchbuttonclicked: (Uisearchbar *) searchbar{NSLog (@"---%@", Searchbar.text); [Self.searchbar Resignfirstresponder];//give up the first responder[liveviewareatable SearchDataBySearchString:searchBar.text];    [Self.navigationcontroller Setnavigationbarhidden:no Animated:yes]; [Self Controlaccessoryview:0];//hide the cover layer. }//The method is executed when the search content changes. Useful when you can implement a real-time search- (void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) SearchText; {NSLog (@"Textdidchange---%@", Searchbar.text); [Liveviewareatable SearchDataBySearchString:searchBar.text];//Search TableView Data[Self Controlaccessoryview:0];//hide the cover layer. }

3. Traverse the child controls of the Uisearchbar control so that you can set the appearance for different sub-views.

 for inch [Self.searchbar Subviews]) {        ifclass]]) {            *btn = (UIButton *) SubView;            [BTN Settitle: @" Cancel "   forstate:uicontrolstatenormal];        }      }

Of course, not very whole. is also the level of entry, effective!

Hope to help you!

iOS Development--ui Advanced (19) Introduction to Uisearchbar controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.