UISearchBar控件就是要為你完成搜索功能的一個(gè)專用控件。它集成了很多你意想不到的功能和特點(diǎn)!
首先,還是來普及一下UISearchBar控件API相關(guān)的屬性和方法吧!
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è)置此屬性時(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];// 提供一個(gè)遮蓋視圖
[self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 設(shè)置鍵盤樣式
// 設(shè)置搜索框下邊的分欄條
[self.searchBar setShowsScopeBar:YES];// 是否顯示分欄條
[self.searchBar setScopeButtonTitles:[NSArrayarrayWithObjects:@"Singer",@"Song",@"Album", nil]];// 分欄條,欄目
[self.searchBar setScopeBarBackgroundImage:[UIImage imageNamed:@"image3"]];//分欄條的背景顏色
[self.searchBar setSelectedScopeButtonIndex:1];// 分欄條默認(rèn)選中的按鈕的下標(biāo)
[self.searchBar setShowsBookmarkButton:YES];// 是否顯示右側(cè)的“書圖標(biāo)”
[self.searchBar setShowsCancelButton:YES];// 是否顯示取消按鈕
[self.searchBar setShowsCancelButton:YES animated:YES];
// 是否提供自動修正功能(這個(gè)方法一般都不用的)
[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];
這么多屬性,其實(shí)看起來多,你實(shí)際去操作事件一下,就發(fā)現(xiàn)很簡單的!
絕大多部分都是定義一些外觀的東西!了解了各個(gè)屬性,一定能滿足你設(shè)計(jì)出你想要的外觀效果!!
然后,解釋一下,我個(gè)人覺的比較有趣和重要的屬性!
1.@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;屬性
例如:
[self.searchBar setInputAccessoryView:your_View];// 提供一個(gè)遮蓋視圖
當(dāng)處于UISearchBar焦點(diǎn)狀態(tài)下(輸入框正要輸入內(nèi)容時(shí)),會有一個(gè)遮蓋視圖。
你翻看一下,iPhone手機(jī)上的電話本搜索功能。那個(gè)遮蓋視圖就是一個(gè)半透明的黑色View。
查看了一下API,是iOS 6.0 以及以后,新加入的!
那么就意味這 iOS 6.0 之前的系統(tǒng)是不兼容的。那么怎么才能達(dá)到這個(gè)類似的效果呢?
變通一下,其實(shí),很簡單:仍然設(shè)置一個(gè)按鈕,初始狀態(tài)下,該UIButton控件透明度設(shè)置為0;并且在控件取得焦點(diǎn)時(shí),設(shè)置透明度為1。
小技巧:如果要設(shè)置這個(gè)屬性,那么,就最好定義一個(gè)UIButton控件,這樣,當(dāng)點(diǎn)擊該遮蓋層的話,可以利用按鈕事件,
設(shè)置:[self.searchBar resignFirstResponder];讓搜索框放棄第一焦點(diǎn)。(iPhone電話薄也是這么做的,感覺很人性化)。
迷惑:還有一個(gè)小的問題:當(dāng)我讓UISearchBar顯示取消按鈕時(shí),當(dāng)我讓UISearchBar失去焦點(diǎn)時(shí),我的取消按鈕也不能點(diǎn)擊了。衰啊。
看了一下iPhone電話薄的UISearchBar,竟然可以也,找了很久,都不知道是怎么回事,大概蘋果又開始玩私有API了吧。
解決方法:很暴力,但是很好用!在UISearchBar上原來取消按鈕的位置上覆蓋一個(gè)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];
// 遮罩層(按鈕)-點(diǎn)擊處理事件
- (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;
說到這個(gè)屬性,就是設(shè)置委托了。
UISearchBarDelegate委托定義了很多關(guān)于,搜索框的一些操作數(shù)據(jù)的協(xié)議方法!
先來個(gè),特寫,把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得到焦點(diǎn)并開始編輯時(shí),執(zhí)行該方法
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
[self.searchBar setShowsCancelButton:YES animated:YES];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self controlAccessoryView:0.9];// 顯示遮蓋層。
return YES;
}
// 取消按鈕被按下時(shí),執(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)容變化時(shí),執(zhí)行該方法。很有用,可以實(shí)現(xiàn)時(shí)實(shí)搜索
- (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];
}
}
當(dāng)然,不是很全。也是入門的級別,見效了!
希望對你有所幫助!
本文轉(zhuǎn)自:http://blog.sina.com.cn/s/blog_8f9dbefd0102v2ip.html
posted on 2015-03-04 19:23
王海光 閱讀(871)
評論(0) 編輯 收藏 引用 所屬分類:
IOS