UISearchBar屬性相關(guān)
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];// 初始化,不解釋
[self.searchBar setPlaceholder:@"Search"];// 搜索框的占位符
[self.searchBar setPrompt:@"Prompt"];// 頂部提示文本,相當(dāng)于控件的Title
[self.searchBar setBarStyle:UIBarMetricsDefault];// 搜索框樣式
[self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的顏色,當(dāng)設(shè)置此屬性時,barStyle將失效
[self.searchBar setTranslucent:YES];// 設(shè)置是否透明
[self.searchBar setBackgroundImage:[UIImage imageNamed:@"image0"]];// 設(shè)置背景圖片
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image3"] forState:UIControlStateNormal];// 設(shè)置搜索框中文本框的背景
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image0"] forState:UIControlStateHighlighted];
[self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30, 30)];// 設(shè)置搜索框中文本框的背景的偏移量
[self.searchBar setSearchResultsButtonSelected:NO];// 設(shè)置搜索結(jié)果按鈕是否選中
[self.searchBar setShowsSearchResultsButton:YES];// 是否顯示搜索結(jié)果按鈕
[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30, 0)];// 設(shè)置搜索框中文本框的文本偏移量
[self.searchBar setInputAccessoryView:_btnHide];// 提供一個遮蓋視圖
[self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 設(shè)置鍵盤樣式
// 設(shè)置搜索框下邊的分欄條
[self.searchBar setShowsScopeBar:YES];// 是否顯示分欄條
[self.searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"Singer",@"Song",@"Album", nil]];// 分欄條,欄目
[self.searchBar setScopeBarBackgroundImage:[UIImage imageNamed:@"image3"]];// 分欄條的背景顏色
[self.searchBar setSelectedScopeButtonIndex:1];// 分欄條默認選中的按鈕的下標
[self.searchBar setShowsBookmarkButton:YES];// 是否顯示右側(cè)的“書圖標”
[self.searchBar setShowsCancelButton:YES];// 是否顯示取消按鈕
[self.searchBar setShowsCancelButton:YES animated:YES];
// 是否提供自動修正功能(這個方法一般都不用的)
[self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];// 設(shè)置自動檢查的類型
[self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];// 是否提供自動修正功能,一般設(shè)置為UITextAutocorrectionTypeDefault
self.searchBar.delegate = self;// 設(shè)置代理
[self.searchBar sizeToFit];
myTableView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0);
[self.view addSubview:myTableView];
[myTableView addSubview:self.searchBar];
這么多屬性,其實看起來多,你實際去操作事件一下,就發(fā)現(xiàn)很簡單的!
絕大多部分都是定義一些外觀的東西!了解了各個屬性,一定能滿足你設(shè)計出你想要的外觀效果!!
然后,解釋一下,我個人覺的比較有趣和重要的屬性!
1.@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;屬性
例如:
[self.searchBar setInputAccessoryView:your_View];// 提供一個遮蓋視圖
當(dāng)處于UISearchBar焦點狀態(tài)下(輸入框正要輸入內(nèi)容時),會有一個遮蓋視圖。
你翻看一下,iPhone手機上的電話本搜索功能。那個遮蓋視圖就是一個半透明的黑色View。
查看了一下API,是iOS 6.0 以及以后,新加入的!
那么就意味這 iOS 6.0 之前的系統(tǒng)是不兼容的。那么怎么才能達到這個類似的效果呢?
變通一下,其實,很簡單:仍然設(shè)置一個按鈕,初始狀態(tài)下,該UIButton控件透明度設(shè)置為0;并且在控件取得焦點時,設(shè)置透明度為1。
小技巧:如果要設(shè)置這個屬性,那么,就最好定義一個UIButton控件,這樣,當(dāng)點擊該遮蓋層的話,可以利用按鈕事件,
設(shè)置:[self.searchBar resignFirstResponder];讓搜索框放棄第一焦點。(iPhone電話薄也是這么做的,感覺很人性化)。
迷惑:還有一個小的問題:當(dāng)我讓UISearchBar顯示取消按鈕時,當(dāng)我讓UISearchBar失去焦點時,我的取消按鈕也不能點擊了。衰啊。
看了一下iPhone電話薄的UISearchBar,竟然可以也,找了很久,都不知道是怎么回事,大概蘋果又開始玩私有API了吧。
解決方法:很暴力,但是很好用!在UISearchBar上原來取消按鈕的位置上覆蓋一個UIButton,設(shè)置成一樣的。呵呵。可以了。
類似如下:
// 遮蓋層
_btnAccessoryView=[[UIButton alloc] initWithFrame:CGRectMake(0, 44, BOUNDS_WIDTH, BOUNDS_HEIGHT)];
[_btnAccessoryView setBackgroundColor:[UIColor blackColor]];
[_btnAccessoryView setAlpha:0.0f];
[_btnAccessoryView addTarget:self action:@selector(ClickControlAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btnAccessoryView];
// 遮罩層(按鈕)-點擊處理事件
-
(void) ClickControlAction:(id)sender{
NSLog(@"handleTaps");[self controlAccessoryView:0];
}
// 控制遮罩層的透明度
-
(void)controlAccessoryView:(float)alphaValue{
[UIView animateWithDuration:0.2 animations:^{
//動畫代碼
[self.btnAccessoryView setAlpha:alphaValue];
}completion:^(BOOL finished){
if (alphaValue<=0) {
[self.searchBar resignFirstResponder];
[self.searchBar setShowsCancelButton:NO animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];}
}];
}
2.@property(nonatomic,assign) id<</b>UISearchBarDelegate> delegate;屬性
例如:
self.searchBar.delegate = self;
說到這個屬性,就是設(shè)置委托了。
UISearchBarDelegate委托定義了很多關(guān)于,搜索框的一些操作數(shù)據(jù)的協(xié)議方法!
先來個,特寫,把x協(xié)議的家庭成員列出來:
@protocol UISearchBarDelegate
@optional
(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
(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;
(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;
(void)searchBarCancelButtonClicked:(UISearchBar *) searchBar;
(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar;
(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;
@end
這不需要解釋吧,看方法名稱就能了解!
我們來看一看,常用的委托方法吧。
pragma mark - UISearchBarDelegate 協(xié)議
// UISearchBar得到焦點并開始編輯時,執(zhí)行該方法
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
[self.searchBar setShowsCancelButton:YES animated:YES];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self controlAccessoryView:0.9];// 顯示遮蓋層。
return YES;
}
// 取消按鈕被按下時,執(zhí)行的方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self.searchBar resignFirstResponder];
[self.searchBar setShowsCancelButton:NO animated:YES];
[liveViewAreaTable searchDataBySearchString:nil];// 搜索tableView數(shù)據(jù)
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self controlAccessoryView:0];// 隱藏遮蓋層。
}
// 鍵盤中,搜索按鈕被按下,執(zhí)行的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(@"---%@",searchBar.text);
[self.searchBar resignFirstResponder];// 放棄第一響應(yīng)者
[liveViewAreaTable searchDataBySearchString:searchBar.text];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self controlAccessoryView:0];// 隱藏遮蓋層。
}
// 當(dāng)搜索內(nèi)容變化時,執(zhí)行該方法。很有用,可以實現(xiàn)時實搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{
NSLog(@"textDidChange---%@",searchBar.text);
[liveViewAreaTable searchDataBySearchString:searchBar.text];// 搜索tableView數(shù)據(jù)
[self controlAccessoryView:0];// 隱藏遮蓋層。
}
3.遍歷UISearchBar控件的子控件,這樣可以針對不同的子視圖來設(shè)置外觀了。
for(id subView in [self.searchBar subviews]){
if([subView isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton *)subView;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
1 // 改變cannel按鈕的文字,7.0
2 UIView *subView0 = searchBar.subviews[0]; // IOS7.0中searchBar組成復(fù)雜點
3 if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
4 for (UIView *subView in subView0.subviews)
5 {
6 // 獲得UINavigationButton按鈕,也就是Cancel按鈕對象,并修改此按鈕的各項屬性
7 if ([subView isKindOfClass:[UIButtonclass]]) {
8 UIButton *cannelButton = (UIButton*)subView;
9 [cannelButton setTitle:@"取消"forState:UIControlStateNormal];
10 [cannelButton setTintColor:[UIColorredColor]];
11 break;
12 }
13 }
14
15 }
16
//初始化搜索控制器對象
searchCTL = [[UISearchController alloc]initWithSearchResultsController:nil];
//設(shè)置代理
searchCTL.searchResultsUpdater = self;
//將搜索結(jié)果的視圖控制器的背景顏色(黑色半透明)去掉
//如果搜索結(jié)果的視圖控制器的背景顏色存在 那么顯示搜索結(jié)果的單元格不能與用戶交互(不能點擊)
searchCTL.dimsBackgroundDuringPresentation = NO;
//將searchBar添加到表格的頭視圖位置
//searchBar是UISearchController上的視圖
table.tableHeaderView = searchCTL.searchBar;
//設(shè)置搜索框中的首字母的大小寫(默認大寫)
/*
UITextAutocapitalizationTypeNone,
UITextAutocapitalizationTypeWords,
UITextAutocapitalizationTypeSentences,
UITextAutocapitalizationTypeAllCharacters,
*/
searchCTL.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController