1.UISearchBar的內(nèi)部結構
由此可以知道其內(nèi)部UISearchBar層次結構為:
2.UISearchBar是不能直接設置其文字和占位文字的大小和顏色的走越,可以通過kvc來獲取子控件,來改變其屬性耻瑟。
首先可以打斷點來獲取其內(nèi)部一些屬性和方法:
因此我們可以用“_searchField”key來獲取其內(nèi)部的UITextField旨指,來設置其文字和占位文字大小和顏色。
UISearchBar * searchView = [[UISearchBar alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.nameLabel.frame), CGRectGetWidth(self.view.frame)-40, 30)];
UITextField * searchField = [searchView valueForKey:@"_searchField"];
if (searchField) {
searchField.font = [UIFont systemFontOfSize:12.0f];
[searchField setValue:[UIFont systemFontOfSize:12.0f] forKeyPath:@"_placeholderLabel.font"];
}
3.UISearchBar文字為空時喳整,搜索按鈕為灰色不可點谆构,有時候我們希望可以點擊收起鍵盤請求數(shù)據(jù)。同上面kvc方法獲取searchField框都,然后設置其enablesReturnKeyAutomatically
為NO
即可搬素。設置后搜索按鈕的狀態(tài)就不可改變啦。
searchField.enablesReturnKeyAutomatically = NO;
4.UISearchBar點擊x按鈕(clearBtn)收鍵盤
UISearchBar沒文字的時候x按鈕會隱藏魏保,我們有時候是希望在清除文字后來收起鍵盤請求數(shù)據(jù)熬尺,但并沒有提供點擊x按鈕響應的事件方法。但是有個代理方法textDidChange:
在文字改變時會一直調(diào)用谓罗,所以我們可以在該方法中通過判斷文字長度為0時粱哼,來收起鍵盤。
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if (searchText.length == 0) {
[searchBar performSelector: @selector(resignFirstResponder)
withObject: nil
afterDelay: 0];
}
}
注意:直接調(diào)用resignFirstResponder
無效妥衣,用performSelector
間接調(diào)用才有效皂吮。
5.收起鍵盤其實上面也提到了調(diào)用resignFirstResponder
注銷第一響應者即可,但有時候我們在收起鍵盤的時候需要做一些事情如過濾篩選數(shù)據(jù)戒傻,請求數(shù)據(jù)等,這時候回去調(diào)用對應的代理方法searchBarTextDidEndEditing:
蜂筹。
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
//獲取篩選字符串
self.likeStr = searchBar.text;
//請求數(shù)據(jù)
[self requestData];
}
6.一些常用的屬性:
- 搜索框風格
// 搜索框風格
@property(nonatomic) UIBarStyle barStyle;
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault //白色搜索框需纳,灰色背景
UIBarStyleBlack //黑色搜索框,
UIBarStyleBlackOpaque = 1, // 禁用. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2, // 禁用. Use UIBarStyleBlack and set the translucent property to YES
}
- 搜索的文本、搜索框頂部的提示信息艺挪、占位符
// 搜索的文本
@property(nullable,nonatomic,copy) NSString *text;
// 搜索框頂部的提示信息
@property(nullable,nonatomic,copy) NSString *prompt;
// 占位符不翩,默認nil, 若有值則在輸入文本后消失
@property(nullable,nonatomic,copy) NSString *placeholder;
- 搜索框右側的按鈕
// 搜索框右側是否顯示圖書按鈕
@property(nonatomic) BOOL showsBookmarkButton;
//搜索框右側是否顯示取消按鈕
@property(nonatomic) BOOL showsCancelButton;
//搜索框右側是否顯示搜索結果按鈕
@property(nonatomic) BOOL showsSearchResultsButton;
// 搜索結果按鈕為選中狀態(tài)
@property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected;
以上四個屬性的默認值都是 NO
- 風格顏色麻裳、背景顏色
// 風格顏色口蝠,可用于修改輸入框的光標顏色,取消按鈕和選擇欄被選中時候都會變成設置的顏色
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
// 搜索框背景顏色
@property(nullable, nonatomic,strong) UIColor *barTintColor;
- 搜索框樣式
// 搜索框樣式
@property (nonatomic) UISearchBarStyle searchBarStyle;
typedef NS_ENUM(NSUInteger, UISearchBarStyle) {
UISearchBarStyleDefault, // 默認樣式津坑,和UISearchBarStyleProminent 一樣
UISearchBarStyleProminent, // 顯示背景妙蔗,常用在my Mail, Messages and Contacts
UISearchBarStyleMinimal // 不顯示背景,系統(tǒng)自帶的背景色無效疆瑰,自定義的有效眉反,常用在Calendar, Notes and Music
}
- 搜索欄的附件選擇按鈕視圖
// 選擇按鈕試圖的按鈕標題
@property(nullable, nonatomic,copy) NSArray<NSString *> *scopeButtonTitles ;
// 選中的按鈕下標值 ,默認 0. 如果超出范圍則忽略
@property(nonatomic) NSInteger selectedScopeButtonIndex ;
// 是否顯示搜索欄的附件選擇按鈕視圖
@property(nonatomic) BOOL showsScopeBar;
- 搜索框背景圖片穆役、搜索框附屬分欄條的背景顏色
// 搜索框背景圖片
@property(nullable, nonatomic,strong) UIImage *backgroundImage;
// 搜索框附屬分欄條的背景顏色
@property(nullable, nonatomic,strong) UIImage *scopeBarBackgroundImage;
- 索框中文本框的背景偏移量和文本偏移量
// 搜索框中文本框的背景偏移量
@property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment;
// 搜索框中文本框的文本偏移量
@property(nonatomic) UIOffset searchTextPositionAdjustment;
7.一些常用的方法:
// 設置是否動畫效果的顯示或隱藏取消按鈕
- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated ;
// 1.設置搜索框背景圖片
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics ;
// 獲取置搜索框背景圖片
- (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics ;
// 2.設置選擇按鈕視圖的背景圖片
- (void)setScopeBarButtonBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state ;
// 獲取選擇按鈕視圖的背景圖片
- (nullable UIImage *)scopeBarButtonBackgroundImageForState:(UIControlState)state ;
// 3.設置搜索框文本框的背景圖片
- (void)setSearchFieldBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state ;
// 獲取搜索框文本框的背景圖片
- (nullable UIImage *)searchFieldBackgroundImageForState:(UIControlState)state;
/ 設置搜索框的圖標
- (void)setImage:(nullable UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state;
// 獲取搜索框的圖標
- (nullable UIImage *)imageForSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state;
typedef NS_ENUM(NSInteger, UISearchBarIcon) {
UISearchBarIconSearch, // 搜索框放大鏡圖標
UISearchBarIconClear , // 搜索框右側可用于清除輸入的文字的圖標x
UISearchBarIconBookmark , // 搜索框右側的圖書圖標
UISearchBarIconResultsList , // 搜索框右側的搜索結果列表圖標
};
// 設置選擇按鈕視圖的分割線圖片
- (void)setScopeBarButtonDividerImage:(nullable UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;
// 獲取選擇按鈕視圖的分割線圖片
- (nullable UIImage *)scopeBarButtonDividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;
// 設置選擇按鈕視圖的標題樣式
- (void)setScopeBarButtonTitleTextAttributes:(nullable NSDictionary<NSString *, id> *)attributes forState:(UIControlState)state;
// 獲取選擇按鈕視圖的標題樣式
- (nullable NSDictionary<NSString *, id> *)scopeBarButtonTitleTextAttributesForState:(UIControlState)state寸五;
// 設置搜索框圖標的偏移量
- (void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon;
// 獲取搜索框圖標的偏移量
- (UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon;
8.UISearchBarDelegate代理方法
// 1. 將要開始編輯文本時會調(diào)用該方法,返回 NO 將不會變成第一響應者
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
// 2. 開始輸入文本會調(diào)用該方法
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;
// 3. 將要結束編輯文本時會調(diào)用該方法耿币,返回 NO 將不會釋放第一響應者
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;
// 4. 結束編輯文本時調(diào)用該方法
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;
// 5. 文本改變會調(diào)用該方法(包含clear文本)
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
// 6. 文字改變前會調(diào)用該方法梳杏,返回NO則不能加入新的編輯文字
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text ;
// 7. 鍵盤上的搜索按鈕點擊的會調(diào)用該方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
// 8. 搜索框右側圖書按鈕點擊會調(diào)用該方法
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar ;
// 9.點擊取消按鈕會調(diào)用該方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;
// 10.搜索結果列表按鈕被按下會調(diào)用該方法
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar ;
// 11. 搜索框的附屬按鈕視圖中切換按鈕會調(diào)用該方法
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;
具體的效果及API解析可參考:iOS--UISearchBar 屬性、方法詳解及應用(自定義搜索框樣式)