初始化:UISearchBar繼承於UIView,咱們能夠像建立View那樣建立searchBar
數組
?ide
1
2
|
UISearchBar * bar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 100, 250, 40)];
[self.view addSubview:bar];
|
@property(nonatomic) UIBarStyle barStyle; 字體
這個屬性能夠設置searchBar的搜索框的風格,枚舉以下:atom
?spa
1
2
3
4
5
6
7
|
typedef
NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0,
//默認風格 白色搜索框,多出的背景爲灰色
UIBarStyleBlack = 1,
//黑色風格,黑色的搜索框
//下面兩個枚舉已經被禁用,做用和黑色風格同樣
UIBarStyleBlackOpaque = 1,
// Deprecated. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2,
// Deprecated. Use UIBarStyleBlack and set the translucent property to YES
};
|
@property(nonatomic,copy) NSString *text; .net
設置搜索框中的文字代理
@property(nonatomic,copy) NSString *prompt; code
這個屬性的官方解釋是在搜索框頂部顯示一行文字,其實就是背景文字,上圖說明:orm
?blog
1
2
|
bar.prompt = @
"搜索框"
;
bar.text=@
"321111111111111111111111111"
|
效果以下:
@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;
設置搜索框總體的風格,枚舉以下:
1
2
3
4
5
|
typedef
NS_ENUM(NSUInteger, UISearchBarStyle) {
UISearchBarStyleDefault,
// currently UISearchBarStyleProminent
UISearchBarStyleProminent,
// 顯示背景
UISearchBarStyleMinimal
// 不顯示背景
} NS_ENUM_AVAILABLE_IOS(7_0);
|
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;
設置是否半透明
@property(nonatomic) BOOL showsScopeBar ;
是否顯示搜索欄的附件選擇按鈕試圖,要想顯示這個試圖,首先要將這個屬性設置爲YES,以後給按鈕數組中添加按鈕,使用下面這個屬性:
@property(nonatomic,copy) NSArray *scopeButtonTitles ;
設置選擇按鈕試圖的按鈕標題
@property(nonatomic) NSInteger selectedScopeButtonIndex;
設置一個默認的選中按鈕
1
2
3
|
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;
設置搜索框的背景圖案
@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;
設置搜索欄中圖片的位置偏移,圖片的枚舉以下:
1
2
3
4
5
6
|
typedef
NS_ENUM(NSInteger, UISearchBarIcon) {
UISearchBarIconSearch,
//搜索圖標
UISearchBarIconClear,
// 清除圖標
UISearchBarIconBookmark,
// 書本圖標
UISearchBarIconResultsList,
// 結果列表圖標
};
|
下面是搜索框控件的一些代理方法:
- (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;
搜索欄的附加試圖中切換按鈕觸發的回調