日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios UISearchBar搜索框的基本使用

發(fā)布時(shí)間:2023/12/9 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios UISearchBar搜索框的基本使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
摘要: 小巧簡潔的原生搜索框,漂亮而易用,如果我們的應(yīng)用沒有特殊需求,都可以使用它。

iOS中UISearchBar(搜索框)使用總結(jié)

初始化:UISearchBar繼承于UIView,我們可以像創(chuàng)建View那樣創(chuàng)建searchBar

????UISearchBar?*?bar?=?[[UISearchBar?alloc]initWithFrame:CGRectMake(20,?100,?250,?40)]; ????[self.view?addSubview:bar];

@property(nonatomic)? ? ? ? UIBarStyle? ? ? ? ? ? ? barStyle;?

這個屬性可以設(shè)置searchBar的搜索框的風(fēng)格,枚舉如下:

typedef?NS_ENUM(NSInteger,?UIBarStyle)?{UIBarStyleDefault??????????=?0,//默認(rèn)風(fēng)格?白色搜索框,多出的背景為灰色 ????UIBarStyleBlack????????????=?1,//黑色風(fēng)格,黑色的搜索框 ????//下面兩個枚舉已經(jīng)被禁用,作用和黑色風(fēng)格一樣 ????UIBarStyleBlackOpaque??????=?1,?//?Deprecated.?Use?UIBarStyleBlack ????UIBarStyleBlackTranslucent?=?2,?//?Deprecated.?Use?UIBarStyleBlack?and?set?the?translucent?property?to?YES };

?

@property(nonatomic,copy) ? NSString ? ? ? ? ? ? ? *text;?

設(shè)置搜索框中的文字

?

@property(nonatomic,copy) ? NSString ? ? ? ? ? ? ? *prompt;?

這個屬性的官方解釋是在搜索框頂部顯示一行文字,其實(shí)就是背景文字,上圖說明:

???bar.prompt?=?@"搜索框";bar.text=@"321111111111111111111111111"

效果如下:

?

@property(nonatomic,copy) ? NSString ? ? ? ? ? ? ? *placeholder; ?

和其他文本輸入控件的placeholder相同,在輸入文字時(shí)就會消失

@property(nonatomic)? ? ? ? BOOL? ? ? ? ? ? ? ? ? ? showsBookmarkButton;?

是否在搜索框右側(cè)顯示一個圖書的按鈕,默認(rèn)為NO,YES的效果如下:

@property(nonatomic)? ? ? ? BOOL? ? ? ? ? ? ? ? ? ? showsCancelButton;

是否顯示取消按鈕,默認(rèn)為NO,YES的效果如下:

@property(nonatomic)? ? ? ? BOOL? ? ? ? ? ? ? ? ? ? showsSearchResultsButton;

是否顯示搜索結(jié)果按鈕,默認(rèn)為NO,YES效果如下:

?

@property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected ;

設(shè)置搜索結(jié)果按鈕的選中狀態(tài)

- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated;

設(shè)置顯示取消按鈕

@property(nonatomic,retain) UIColor *tintColor;

設(shè)置這個顏色值會影響搜索框中的光標(biāo)的顏色

?

@property(nonatomic,retain) UIColor *barTintColor;

設(shè)置這個顏色會影響搜索框的背景顏色

?

@property (nonatomic) UISearchBarStyle searchBarStyle;

設(shè)置搜索框整體的風(fēng)格,枚舉如下:

typedef?NS_ENUM(NSUInteger,?UISearchBarStyle)?{UISearchBarStyleDefault,????//?currently?UISearchBarStyleProminent ????UISearchBarStyleProminent,??//?顯示背景 ????UISearchBarStyleMinimal?????//?不顯示背景 }?NS_ENUM_AVAILABLE_IOS(7_0);

?

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;

設(shè)置是否半透明

@property(nonatomic)? ? ? BOOL ? ? ? showsScopeBar ;

是否顯示搜索欄的附件選擇按鈕試圖,要想顯示這個試圖,首先要將這個屬性設(shè)置為YES,之后給按鈕數(shù)組中添加按鈕,使用下面這個屬性:

@property(nonatomic,copy) NSArray ? *scopeButtonTitles ;

設(shè)置選擇按鈕試圖的按鈕標(biāo)題

@property(nonatomic)? ? ? NSInteger? selectedScopeButtonIndex;

設(shè)置一個默認(rèn)的選中按鈕

????bar?=?[[UISearchBar?alloc]initWithFrame:CGRectMake(20,?100,?250,?200)]; ????bar.showsScopeBar=YES; ????bar.scopeButtonTitles?=?@[@"12",@"2",@"3",@"4"];

@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;

鍵盤的附屬試圖

@property(nonatomic,retain) UIImage *backgroundImage;

設(shè)置搜索框的背景圖案

@property(nonatomic,retain) UIImage *scopeBarBackgroundImage;

設(shè)置附屬選擇按鈕視圖的背景圖案

?

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics ;??

- (UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics

這一對方法可以設(shè)置和獲取某個狀態(tài)枚舉下的搜索框的背景圖案

?

?

- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state;

- (UIImage *)searchFieldBackgroundImageForState:(UIControlState)state;

這一對方法用于設(shè)置和獲取搜索框中TextField的背景圖案

?

- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state ;

- (UIImage *)imageForSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state ;

這一對方法用于獲取和設(shè)置搜索欄icon圖片的圖案

- (void)setScopeBarButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state;?

- (UIImage *)scopeBarButtonBackgroundImageForState:(UIControlState)state;

這一對方法用于設(shè)置和獲取搜索框的附加選擇按鈕視圖的背景圖案

?

?

- (void)setScopeBarButtonDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;

- (UIImage *)scopeBarButtonDividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;

這一對方法用于獲取和設(shè)置附加選擇按鈕視圖中切換按鈕的圖案

?

- (void)setScopeBarButtonTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state;

- (NSDictionary *)scopeBarButtonTitleTextAttributesForState:(UIControlState)state;

這一對方法用于設(shè)置和獲取切換按鈕標(biāo)題文字的字體屬性字典

?

?

@property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment;

搜索文字在搜索框中的位置偏移

@property(nonatomic) UIOffset searchTextPositionAdjustment;

textfield在搜索框中的位置偏移

?

- (void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon;

- (UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon;

設(shè)置搜索欄中圖片的位置偏移,圖片的枚舉如下:

typedef?NS_ENUM(NSInteger,?UISearchBarIcon)?{ ????UISearchBarIconSearch,?//搜索圖標(biāo) ????UISearchBarIconClear,?//?清除圖標(biāo) ????UISearchBarIconBookmark,?//?書本圖標(biāo) ????UISearchBarIconResultsList,?//?結(jié)果列表圖標(biāo) };

?

下面是搜索框控件的一些代理方法:

?

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; ? ? ? ? ??

將要開始編輯時(shí)的回調(diào),返回為NO,則不能編輯

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar; ? ? ? ? ? ? ? ? ?

已經(jīng)開始編輯時(shí)的回調(diào)

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; ? ? ? ? ? ? ? ?

將要結(jié)束編輯時(shí)的回調(diào)

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; ? ? ? ? ? ? ? ? ??

已經(jīng)結(jié)束編輯的回調(diào)

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; ? 編輯文字改變的回調(diào)

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text ;?

編輯文字改變前的回調(diào),返回NO則不能加入新的編輯文字

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; ? ? ??

搜索按鈕點(diǎn)擊的回調(diào)

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar; ? ? ? ? ? ??

書本按鈕點(diǎn)擊的回調(diào)

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar; ? ? ? ? ? ? ??

取消按鈕點(diǎn)擊的回調(diào)

- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar;?

搜索結(jié)果按鈕點(diǎn)擊的回調(diào)

?

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;

搜索欄的附加試圖中切換按鈕觸發(fā)的回調(diào)

總結(jié)

以上是生活随笔為你收集整理的ios UISearchBar搜索框的基本使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。