初始化:uisearchbar繼承于uiview,我們可以像建立view那樣建立searchbar
<a href="http://my.oschina.net/u/2340880/blog/509756#">?</a>
1
2
<code> </code><code>uisearchbar * bar = [[uisearchbar alloc]initwithframe:cgrectmake(20, 100, 250, 40)];</code>
<code> </code><code>[self.view addsubview:bar];</code>
@property(nonatomic) uibarstyle barstyle;
這個屬性可以設定searchbar的搜尋框的風格,枚舉如下:
3
4
5
6
7
<code>typedef</code> <code>ns_enum(nsinteger, uibarstyle) {</code>
<code> </code><code>uibarstyledefault = 0,</code><code>//預設風格 白色搜尋框,多出的背景為灰色</code>
<code> </code><code>uibarstyleblack = 1,</code><code>//黑色風格,黑色的搜尋框</code>
<code> </code><code>//下面兩個枚舉已經被禁用,作用和黑色風格一樣</code>
<code> </code><code>uibarstyleblackopaque = 1, </code><code>// deprecated. use uibarstyleblack</code>
<code> </code><code>uibarstyleblacktranslucent = 2, </code><code>// deprecated. use uibarstyleblack and set the translucent property to yes</code>
<code>};</code>
@property(nonatomic,copy) nsstring *text;
設定搜尋框中的文字
@property(nonatomic,copy) nsstring *prompt;
這個屬性的官方解釋是在搜尋框頂部顯示一行文字,其實就是背景文字,上圖說明:
<code> </code><code>bar.prompt = @</code><code>"搜尋框"</code><code>;</code>
<code> </code><code>bar.text=@</code><code>"321111111111111111111111111"</code>
效果如下:
@property(nonatomic,copy) nsstring *placeholder;
和其他文本輸入控件的placeholder相同,在輸入文字時就會消失
@property(nonatomic) bool showsbookmarkbutton;
是否在搜尋框右側顯示一個圖書的按鈕,預設為no,yes的效果如下:
@property(nonatomic) bool showscancelbutton;
是否顯示取消按鈕,預設為no,yes的效果如下:
@property(nonatomic) bool showssearchresultsbutton;
是否顯示搜尋結果按鈕,預設為no,yes效果如下:
@property(nonatomic, getter=issearchresultsbuttonselected) bool searchresultsbuttonselected ;
設定搜尋結果按鈕的選中狀态
- (void)setshowscancelbutton:(bool)showscancelbutton animated:(bool)animated;
設定顯示取消按鈕
@property(nonatomic,retain) uicolor *tintcolor;
設定這個顔色值會影響搜尋框中的光标的顔色
@property(nonatomic,retain) uicolor *bartintcolor;
設定這個顔色會影響搜尋框的背景顔色
@property (nonatomic) uisearchbarstyle searchbarstyle;
設定搜尋框整體的風格,枚舉如下:
<code>typedef</code> <code>ns_enum(nsuinteger, uisearchbarstyle) {</code>
<code> </code><code>uisearchbarstyledefault, </code><code>// currently uisearchbarstyleprominent</code>
<code> </code><code>uisearchbarstyleprominent, </code><code>// 顯示背景</code>
<code> </code><code>uisearchbarstyleminimal </code><code>// 不顯示背景</code>
<code>} ns_enum_available_ios(7_0);</code>
@property(nonatomic,assign,getter=istranslucent) bool translucent;
設定是否半透明
@property(nonatomic) bool showsscopebar ;
是否顯示搜尋欄的附件選擇按鈕試圖,要想顯示這個試圖,首先要将這個屬性設定為yes,之後給按鈕數組中添加按鈕,使用下面這個屬性:
@property(nonatomic,copy) nsarray *scopebuttontitles ;
設定選擇按鈕試圖的按鈕标題
@property(nonatomic) nsinteger selectedscopebuttonindex;
設定一個預設的選中按鈕
<code> </code><code>bar = [[uisearchbar alloc]initwithframe:cgrectmake(20, 100, 250, 200)];</code>
<code> </code><code>bar.showsscopebar=yes;</code>
<code> </code><code>bar.scopebuttontitles = @[@</code><code>"12"</code><code>,@</code><code>"2"</code><code>,@</code><code>"3"</code><code>,@</code><code>"4"</code><code>];</code>
@property (nonatomic, readwrite, retain) uiview *inputaccessoryview;
鍵盤的附屬試圖
@property(nonatomic,retain) uiimage *backgroundimage;
設定搜尋框的背景圖案
@property(nonatomic,retain) uiimage *scopebarbackgroundimage;
設定附屬選擇按鈕視圖的背景圖案
- (void)setbackgroundimage:(uiimage *)backgroundimage forbarposition:(uibarposition)barposition barmetrics:(uibarmetrics)barmetrics ;
- (uiimage *)backgroundimageforbarposition:(uibarposition)barposition barmetrics:(uibarmetrics)barmetrics
這一對方法可以設定和擷取某個狀态枚舉下的搜尋框的背景圖案
- (void)setsearchfieldbackgroundimage:(uiimage *)backgroundimage forstate:(uicontrolstate)state;
- (uiimage *)searchfieldbackgroundimageforstate:(uicontrolstate)state;
這一對方法用于設定和擷取搜尋框中textfield的背景圖案
- (void)setimage:(uiimage *)iconimage forsearchbaricon:(uisearchbaricon)icon state:(uicontrolstate)state ;
- (uiimage *)imageforsearchbaricon:(uisearchbaricon)icon state:(uicontrolstate)state ;
這一對方法用于擷取和設定搜尋欄icon圖檔的圖案
- (void)setscopebarbuttonbackgroundimage:(uiimage *)backgroundimage forstate:(uicontrolstate)state;
- (uiimage *)scopebarbuttonbackgroundimageforstate:(uicontrolstate)state;
這一對方法用于設定和擷取搜尋框的附加選擇按鈕視圖的背景圖案
- (void)setscopebarbuttondividerimage:(uiimage *)dividerimage forleftsegmentstate:(uicontrolstate)leftstate rightsegmentstate:(uicontrolstate)rightstate;
- (uiimage *)scopebarbuttondividerimageforleftsegmentstate:(uicontrolstate)leftstate rightsegmentstate:(uicontrolstate)rightstate;
這一對方法用于擷取和設定附加選擇按鈕視圖中切換按鈕的圖案
- (void)setscopebarbuttontitletextattributes:(nsdictionary *)attributes forstate:(uicontrolstate)state;
- (nsdictionary *)scopebarbuttontitletextattributesforstate:(uicontrolstate)state;
這一對方法用于設定和擷取切換按鈕标題文字的字型屬性字典
@property(nonatomic) uioffset searchfieldbackgroundpositionadjustment;
搜尋文字在搜尋框中的位置偏移
@property(nonatomic) uioffset searchtextpositionadjustment;
textfield在搜尋框中的位置偏移
- (void)setpositionadjustment:(uioffset)adjustment forsearchbaricon:(uisearchbaricon)icon;
- (uioffset)positionadjustmentforsearchbaricon:(uisearchbaricon)icon;
設定搜尋欄中圖檔的位置偏移,圖檔的枚舉如下:
<code>typedef</code> <code>ns_enum(nsinteger, uisearchbaricon) {</code>
<code> </code><code>uisearchbariconsearch, </code><code>//搜尋圖示</code>
<code> </code><code>uisearchbariconclear, </code><code>// 清除圖示</code>
<code> </code><code>uisearchbariconbookmark, </code><code>// 書本圖示</code>
<code> </code><code>uisearchbariconresultslist, </code><code>// 結果清單圖示</code>
下面是搜尋框控件的一些代理方法:
- (bool)searchbarshouldbeginediting:(uisearchbar *)searchbar;
将要開始編輯時的回調,傳回為no,則不能編輯
- (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 ;
編輯文字改變前的回調,傳回no則不能加入新的編輯文字
- (void)searchbarsearchbuttonclicked:(uisearchbar *)searchbar;
搜尋按鈕點選的回調
- (void)searchbarbookmarkbuttonclicked:(uisearchbar *)searchbar;
書本按鈕點選的回調
- (void)searchbarcancelbuttonclicked:(uisearchbar *)searchbar;
取消按鈕點選的回調
- (void)searchbarresultslistbuttonclicked:(uisearchbar *)searchbar;
搜尋結果按鈕點選的回調
- (void)searchbar:(uisearchbar *)searchbar selectedscopebuttonindexdidchange:(nsinteger)selectedscope;
搜尋欄的附加試圖中切換按鈕觸發的回調