開發中經常要用到搜索框這個控件,如下圖所示
很多人不熟悉怎么使用搜索框,本文和大家一起學習系統提供的UISearchBar怎么用,UISearchBar、UISearchController、UISearchContainerViewController、UISearchDisplayController分別是什么,以及該在什么時候使用自定義的搜索框,此篇先研究UISearchBar的使用
1. UISearchBar是什么
UISearchBar和UIButton一樣,是系統已經提供給我們使用的用于輸入文字進行搜索的一個搜索框,由一個文本框和幾個按鈕組成,當用戶在文本框內輸入部分內容之后,程序即可按照指定的規則執行搜索。
UISearchBar使用
最簡單的創建方法
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
[self.view addSubview:searchBar];
效果進入UISearchBar頭文件,我們解剖一下它的構成
- UISearchBarIcon 枚舉類型,用于識別搜索欄中使用的圖標的常量。表示UISearchBar相關子控件Icon,可以在設置子控件Icon時使用
UISearchBarIconSearch, // 搜索圖標,放大鏡
UISearchBarIconClear __TVOS_PROHIBITED, // 清除圖標,里面有一個X的圓
UISearchBarIconBookmark __TVOS_PROHIBITED, // 書簽圖標,打開書圖標
UISearchBarIconResultsList __TVOS_PROHIBITED, // 結果列表圖標,名單菱形圖標
- UISearchBarStyle 枚舉類型,指定搜索欄背景樣式。表示UISearchBar樣式
UISearchBarStyleDefault, // 默認樣式,即UISearchBarStyleProminent
UISearchBarStyleProminent, // 類似于通訊錄搜索欄,搜索欄具有半透明背景,搜索區域不透明。
UISearchBarStyleMinimal // 類似于音樂搜索欄,搜索欄沒有背景,搜索字段是半透明的。
-
常見的或經常使用的相關屬性
-
@property(nonatomic) UIBarStyle barStyle; 這個屬性可以設置searchBar的搜索框的背景風格,枚舉如下:
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(nullable,nonatomic,weak) id<UISearchBarDelegate> delegate; 設置searchBar的代理,用于搜索時調用一些代理方法
- @property(nullable,nonatomic,copy) NSString *text; 設置搜索框中的文字
-
@property(nullable,nonatomic,copy) NSString *prompt; 在搜索欄頂部顯示的單行文本。默認為空,官方解釋是在搜索框頂部顯示一行文字,其實就是背景文字,上圖說明:
- @property(nullable,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; 設置搜索結果按鈕的選中狀態,設為YES,則按鈕為選中的藍色狀態
- @property (nonatomic, readonly, strong) UITextInputAssistantItem *inputAssistantItem;用于配置鍵盤快捷鍵的輸入助手,只讀屬性,不能設置更改
- @property(null_resettable, nonatomic,strong) UIColor *tintColor; 設置這個顏色值會影響搜索框中的光標的顏色
- @property(nullable, nonatomic,strong) UIColor *barTintColor; 設置這個顏色會影響搜索框的背景顏色
- @property (nonatomic) UISearchBarStyle searchBarStyle; 設置搜索框的風格
- @property(nonatomic,assign,getter=isTranslucent) BOOL translucent; 設置是否半透明
- @property(nullable, nonatomic,copy) NSArray<NSString *> *scopeButtonTitles; 設置選擇按鈕試圖的按鈕標題
- @property(nonatomic) NSInteger selectedScopeButtonIndex; 設置一個默認的選中按鈕
- @property(nonatomic) BOOL showsScopeBar; 是否顯示搜索欄的附件選擇按鈕試圖,要想顯示這個試圖,首先要將這個屬性設置為YES,之后給按鈕數組中添加按鈕,使用scopeButtonTitles屬性和selectedScopeButtonIndex屬性
- @property (nullable, nonatomic, readwrite, strong) UIView *inputAccessoryView; 鍵盤的附屬視圖
- @property(nullable, nonatomic,strong) UIImage *backgroundImage; 設置搜索框的背景圖案
- @property(nullable, nonatomic,strong) UIImage *scopeBarBackgroundImage; 設置附屬選擇按鈕視圖的背景圖案
- @property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment; textfield在搜索框中的位置偏移
- @property(nonatomic) UIOffset searchTextPositionAdjustment; 搜索文字在搜索框中的位置偏移
-
-
常見的或經常使用的方法
-
- (instancetype)init;
- (instancetype)initWithFrame:(CGRect)frame;
初始化創建方法 -
- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated
帶動畫設置取消按鈕的顯示狀態 -
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics;
設置在給定位置和給定度量的背景圖像,用于設置某個狀態枚舉下的搜索框的背景圖案 -
- (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics;
返回給定位置和給定度量值中用于背景的圖像,獲取某個狀態枚舉下的搜索框的背景圖案 -
- (void)setSearchFieldBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state;
設置給定狀態搜索文本框圖像,用于設置搜索框中TextField的背景圖案 -
- (nullable 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;
這一對方法用于設置和獲取切換按鈕標題文字的字體屬性字典 -
- (void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon;
- (UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon;
設置和獲取搜索欄中圖片的位置偏移
-
-
常見的或經常使用的delegate代理方法
-
- (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;
搜索欄的附加試圖中切換按鈕觸發的回調
-