根本屬性
@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]);
}
}
}
【iOS App開辟中UISearchBar搜刮欄組件的根本用法整頓】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!