UISearchBar控件就是要為你完成搜索功能的一個專用控件。它集成了很多你意想不到的功能和特點!
首先,還是來普及一下UISearchBar控件API相關的屬性和方法吧!
UISearchBar屬性相關
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];// 初始化,不解釋
[self.searchBar setPlaceholder:@"Search"];// 搜索框的占位符
[self.searchBar setPrompt:@"Prompt"];// 頂部提示文本,相當于控件的Title
[self.searchBar setBarStyle:UIBarMetricsDefault];// 搜索框樣式
[self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的顏色,當設置此屬性時,barStyle將失效
[self.searchBar setTranslucent:YES];// 設置是否透明
[self.searchBar setBackgroundImage:[UIImage imageNamed:@"image0"]];// 設置背景圖片
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image3"]forState:UIControlStateNormal];// 設置搜索框中文本框的背景
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image0"]forState:UIControlStateHighlighted];
[self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30,30)];// 設置搜索框中文本框的背景的偏移量
[self.searchBar setSearchResultsButtonSelected:NO];// 設置搜索結果按鈕是否選中
[self.searchBar setShowsSearchResultsButton:YES];// 是否顯示搜索結果按鈕
[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30, 0)];// 設置搜索框中文本框的文本偏移量
[self.searchBar setInputAccessoryView:_btnHide];// 提供一個遮蓋視圖
[self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 設置鍵盤樣式
// 設置搜索框下邊的分欄條
[self.searchBar setShowsScopeBar:YES];// 是否顯示分欄條
[self.searchBar setScopeButtonTitles:[NSArrayarrayWithObjects:@"Singer",@"Song",@"Album", nil]];// 分欄條,欄目
[self.searchBar setScopeBarBackgroundImage:[UIImage imageNamed:@"image3"]];//分欄條的背景顏色
[self.searchBar setSelectedScopeButtonIndex:1];// 分欄條默認選中的按鈕的下標
[self.searchBar setShowsBookmarkButton:YES];// 是否顯示右側的“書圖標”
[self.searchBar setShowsCancelButton:YES];// 是否顯示取消按鈕
[self.searchBar setShowsCancelButton:YES animated:YES];
// 是否提供自動修正功能(這個方法一般都不用的)
[self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];// 設置自動檢查的類型
[self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];// 是否提供自動修正功能,一般設置為UITextAutocorrectionTypeDefault
self.searchBar.delegate = self;// 設置代理
[self.searchBar sizeToFit];
myTableView.contentInset =UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0);
[self.view addSubview:myTableView];
[myTableView addSubview:self.searchBar];
這么多屬性,其實看起來多,你實際去操作事件一下,就發現很簡單的!
絕大多部分都是定義一些外觀的東西!了解了各個屬性,一定能滿足你設計出你想要的外觀效果!!
然后,解釋一下,我個人覺的比較有趣和重要的屬性!
1.@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;屬性
例如:
[self.searchBar setInputAccessoryView:your_View];// 提供一個遮蓋視圖
當處于UISearchBar焦點狀態下(輸入框正要輸入內容時),會有一個遮蓋視圖。
你翻看一下,iPhone手機上的電話本搜索功能。那個遮蓋視圖就是一個半透明的黑色View。
查看了一下API,是iOS 6.0 以及以后,新加入的!
那么就意味這 iOS 6.0 之前的系統是不兼容的。那么怎么才能達到這個類似的效果呢?
變通一下,其實,很簡單:仍然設置一個按鈕,初始狀態下,該UIButton控件透明度設置為0;并且在控件取得焦點時,設置透明度為1。
小技巧:如果要設置這個屬性,那么,就最好定義一個UIButton控件,這樣,當點擊該遮蓋層的話,可以利用按鈕事件,
設置:[self.searchBar resignFirstResponder];讓搜索框放棄第一焦點。(iPhone電話薄也是這么做的,感覺很人性化)。
迷惑:還有一個小的問題:當我讓UISearchBar顯示取消按鈕時,當我讓UISearchBar失去焦點時,我的取消按鈕也不能點擊了。衰啊。
看了一下iPhone電話薄的UISearchBar,竟然可以也,找了很久,都不知道是怎么回事,大概蘋果又開始玩私有API了吧。
解決方法:很暴力,但是很好用!在UISearchBar上原來取消按鈕的位置上覆蓋一個UIButton,設置成一樣的。呵呵。可以了。
類似如下:
// 遮蓋層
_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;
說到這個屬性,就是設置委托了。
UISearchBarDelegate委托定義了很多關于,搜索框的一些操作數據的協議方法!
先來個,特寫,把x協議的家庭成員列出來:
@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 協議
// UISearchBar得到焦點并開始編輯時,執行該方法
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
[self.searchBar setShowsCancelButton:YES animated:YES];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self controlAccessoryView:0.9];// 顯示遮蓋層。
return YES;
}
// 取消按鈕被按下時,執行的方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self.searchBar resignFirstResponder];
[self.searchBar setShowsCancelButton:NO animated:YES];
[liveViewAreaTable searchDataBySearchString:nil];// 搜索tableView數據
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self controlAccessoryView:0];// 隱藏遮蓋層。
}
// 鍵盤中,搜索按鈕被按下,執行的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(@"---%@",searchBar.text);
[self.searchBar resignFirstResponder];// 放棄第一響應者
[liveViewAreaTable searchDataBySearchString:searchBar.text];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self controlAccessoryView:0];// 隱藏遮蓋層。
}
// 當搜索內容變化時,執行該方法。很有用,可以實現時實搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{
NSLog(@"textDidChange---%@",searchBar.text);
[liveViewAreaTable searchDataBySearchString:searchBar.text];// 搜索tableView數據
[self controlAccessoryView:0];// 隱藏遮蓋層。
}
3.遍歷UISearchBar控件的子控件,這樣可以針對不同的子視圖來設置外觀了。
for(id subView in [self.searchBar subviews]){
if([subView isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton *)subView;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
當然,不是很全。也是入門的級別,見效了!
希望對你有所幫助!
本文轉自:http://blog.sina.com.cn/s/blog_8f9dbefd0102v2ip.html
posted on 2015-03-04 19:23
王海光 閱讀(871)
評論(0) 編輯 收藏 引用 所屬分類:
IOS