基本屬性
復制代碼 代碼如下:
@UISearchBar search = [[UISearchBar alloc]initWithFrame:CGRectMake(0,44,320,120)];
pragma mark -基本設置
復制代碼 代碼如下:
//控件的樣式 默認--0白色,1是黑色風格
/*
UIBarStyleDefault = 0,
UIBarStyleBlack = 1,
search.barStyle =UIBarStyleDefault;
/*
UISearchBarStyleDefault,
// currently UISearchBarStyleProminent
UISearchBarStyleProminent, // used my Mail, Messages and Contacts(provides no default background color or image but will display one if customized as such系統提供的顏色和圖片無效,自定制有效)
UISearchBarStyleMinimal // used by Calendar, Notes and Music
*/
search.searchBarStyle =UISearchBarStyleDefault;
// 控件上面的顯示的文字
search.text =@"HMT";
// 顯示在頂部的單行文字,通常作為一個提示行
search.prompt =@"DOTA";
// 半透明的提示文字,輸入搜索內容消失
search.placeholder =@"請輸入要搜索的詞語";
// bar的顏色(具有漸變效果)搜索欄閃動條和選擇欄邊框,取消按鈕和選擇欄被選中時候都會變成設置的顏色
search.tintColor = [UIColor redColor];
// 除搜索欄框框,就像貼了一張镂空了搜索欄的顏色貼圖,不影響其他任何設置的顏色
search.barTintColor = [UIColor whiteColor];
// 指定控件是否會有透視效果
search.translucent =YES;
// 設置在什麼的情況下自動大寫
/*
UITextAutocapitalizationTypeNone, //除非自己點擊大寫,否則永不大寫
UITextAutocapitalizationTypeWords, //以單詞來區分,每個單詞首字母大寫
UITextAutocapitalizationTypeSentences, //以句子來區分
UITextAutocapitalizationTypeAllCharacters, //所有字母全部大寫
*/
search.autocapitalizationType =UITextAutocapitalizationTypeNone;
// 對於文本對象自動校正風格(額,我也不知道有什麼用)
/*
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
*/
search.autocorrectionType =UITextAutocorrectionTypeNo;
// 鍵盤的樣式(具體可參考文章UITableView詳解(一))
search.keyboardType =UIKeyboardTypeNumberPad;
pragma mark - 設置搜索欄右邊按鈕圖標(UISearchBarIcon)
復制代碼 代碼如下:
// 是否在控件的右端顯示一個書的按鈕
search.showsBookmarkButton =YES;
// 是否顯示cancel按鈕(靜態)
//search.showsCancelButton = YES;
// 是否顯示cancel按鈕(帶有動畫效果)
[search setShowsCancelButton:YES animated:YES];
// 是否在控件的右端顯示搜索結果按鈕(圖形是一個圓裡面放著一個向下的箭頭)
search.showsSearchResultsButton =YES;
// 搜索結果按鈕是否被選中
search.showsSearchResultsButton =YES;
// 設置控件的右端顯示搜索結果按鈕處 --- 可用圖片替換掉
[search setImage:[UIImage imageNamed:@"qiyi.png"]forSearchBarIcon:UISearchBarIconResultsList state:UIControlStateNormal];
pragma mark - 搜索欄下部選擇欄
復制代碼 代碼如下:
// 搜索欄下部的選擇欄,數組裡面的內容是按鈕的標題
search.scopeButtonTitles = [NSArray arrayWithObjects:@"iOS",@"Android",@"iPhone",nil];
// 進入界面,搜索欄下部的默認選擇欄按鈕的索引(也就是第一出現在哪個選擇欄)
search.selectedScopeButtonIndex =2;
// 控制搜索欄下部的選擇欄是否顯示出來(顯示的話,就要修改search的frame,不顯示的話80就夠了)
search.showsScopeBar =YES;
pragma mark - 設置控件圖片
復制代碼 代碼如下:
// 設置控件背景圖片
search.backgroundImage = [UIImage imageNamed:@"qiyi.png"];
// 設置搜索欄下部背景圖片
search.scopeBarBackgroundImage = [UIImage imageNamed:@"qiyi.png"];
pragma mark - 協議UISearchBarDelegate
(不解釋了,看名字,已經很明顯了)
復制代碼 代碼如下:
@編輯文本
// UISearchBar得到焦點並開始編輯時,執行該方法
(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; // return NO to not become first responder
(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ // called when text starts editing
[searchBar setShowsCancelButton:YES animated:YES]; // 動畫顯示取消按鈕
}
(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; // return NO to not resign first responder
(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; // called when text ends editing
(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ // called when text changes (including clear)
@ 當搜索內容變化時,執行該方法。很有用,可以實現時實搜索
}
復制代碼 代碼如下:
(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)textNS_AVAILABLE_IOS(3_0); // called before text changes
@按鈕點擊
(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; // called when keyboard search button pressed
(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar; // called when bookmark button pressed
(void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{ // called when cancel button pressed
[searchBar setShowsCancelButton:NO animated:NO]; // 取消按鈕回收
[searchBar resignFirstResponder]; // 取消第一響應值,鍵盤回收,搜索結束
}
(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBarNS_AVAILABLE_IOS(3_2);// called when search results button pressed
(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScopeNS_AVAILABLE_IOS(3_0);
數據刷選類:NSPredicate
復制代碼 代碼如下:
@假設: NSArray array = [[NSArray alloc]initWithObjects:@"luna",@"moon",@"",@"lion",@"coco", nil];
// 數據的處理主要發生在這個方法中
(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
// 方法一:([c]不區分大小寫[d]不區分發音符號即沒有重音符號[cd]既不區分大小寫,也不區分發音符號。)
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@",searchText];
// 數組提供的快速遍歷,返回的類型是NSArray
NSLog(@"%@",[ _array filteredArrayUsingPredicate:predicate]);
// 方法二:
for (int i = 0; i count]; i++) {
if ([predicate evaluateWithObject:[ _array objectAtIndex:i]]) {
NSLog(@"%@",[arrayobjectAtIndex:i]);
}
}
}