基本屬性
複製代碼 代碼如下:
@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]);
}
}
}