_searchBar = [[UISearchBaralloc]initWithFrame:CGRectZero];//初始化,不解釋
[self.searchBar setPlaceholder:@"Search"];//搜索框的佔位符
[self.searchBarsetPrompt:@"Prompt"];//頂部提示文本,相當於控件的Title
[self.searchBar setBarStyle:UIBarMetricsDefault];//搜索框樣式
[self.searchBarsetTintColor:[UIColorblackColor]];//搜索框的顏色,當設置此屬性時,barStyle將失效
[self.searchBar setTranslucent:YES];//設置是否透明
[self.searchBar setBackgroundImage:[UIImage imageNamed:@"image0"]];//設置背景圖片
[self.searchBarsetSearchFieldBackgroundImage:[UIImage imageNamed:@"image3"] forState:UIControlStateNormal];//設置搜索框中文本框的背景
[self.searchBarsetSearchFieldBackgroundImage:[UIImage imageNamed:@"image0"] forState:UIControlStateHighlighted];
[self.searchBarsetSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30,30)];//設置搜索框中文本框的背景的偏移量
[self.searchBarsetSearchResultsButtonSelected:NO];//設置搜索結果按鈕是否選中
[self.searchBarsetShowsSearchResultsButton:YES];//是否顯示搜索結果按鈕
[self.searchBarsetSearchTextPositionAdjustment:UIOffsetMake(30,0)];//設置搜索框中文本框的文本偏移量
[self.searchBar setInputAccessoryView:_btnHide];//提供一個遮蓋視圖
[self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];//設置鍵盤樣式
//設置搜索框下邊的分欄條
[self.searchBar setShowsScopeBar:YES];//是否顯示分欄條
[self.searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"Singer",@"Song",@"Album",nil]];//分欄條,欄目
[self.searchBarsetScopeBarBackgroundImage:[UIImage imageNamed:@"image3"]];//分欄條的背景顏色
[self.searchBarsetSelectedScopeButtonIndex:1];//分欄條默認選中的按鈕的下標
[self.searchBarsetShowsBookmarkButton:YES];//是否顯示右側的“書圖標”
[self.searchBar setShowsCancelButton:YES];//是否顯示取消按鈕
[self.searchBar setShowsCancelButton:YESanimated:YES];
//是否提供自動修正功能(這個方法一般都不用的)
[self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];//設置自動檢查的類型
[self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];//是否提供自動修正功能,一般設置爲UITextAutocorrectionTypeDefault
self.searchBar.delegate =self;//設置代理
[self.searchBarsizeToFit];
myTableView.contentInset= UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds),0,0,0);
[self.viewaddSubview: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.searchBarresignFirstResponder];讓搜索框放棄第一焦點。(iPhone電話薄也是這麼做的,感覺很人性化)。
迷惑:還有一個小的問題:當我讓UISearchBar顯示取消按鈕時,當我讓UISearchBar失去焦點時,我的取消按鈕也不能點擊了。衰啊。
看了一下iPhone電話薄的UISearchBar,竟然可以也,找了很久,都不知道是怎麼回事,大概蘋果又開始玩私有API了吧。
解決方法:很暴力,但是很好用!在UISearchBar上原來取消按鈕的位置上覆蓋一個UIButton,設置成一樣的。呵呵。可以了。
類似如下:
// 遮蓋層
_btnAccessoryView=[[UIButton alloc] initWithFrame:CGRectMake(0,44,BOUNDS_WIDTH,BOUNDS_HEIGHT)];
[_btnAccessoryViewsetBackgroundColor:[UIColorblackColor]];
[_btnAccessoryView setAlpha:0.0f];
[_btnAccessoryView addTarget:selfaction:@selector(ClickControlAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:_btnAccessoryView];
//遮罩層(按鈕)-點擊處理事件
-(void)ClickControlAction:(id)sender{
NSLog(@"handleTaps");
[selfcontrolAccessoryView:0];
}
//控制遮罩層的透明度
-(void)controlAccessoryView:(float)alphaValue{
[UIViewanimateWithDuration:0.2animations:^{
//動畫代碼
[self.btnAccessoryViewsetAlpha:alphaValue];
}completion:^(BOOL finished){
if(alphaValue<=0){
[self.searchBar resignFirstResponder];
[self.searchBar setShowsCancelButton:NOanimated:YES];
[self.navigationControllersetNavigationBarHidden:NOanimated:YES];
}
}];
}
2.@property(nonatomic,assign)id<</b>UISearchBarDelegate>delegate;屬性
例如:
self.searchBar.delegate= self;
說到這個屬性,就是設置委託了。
UISearchBarDelegate委託定義了很多關於,搜索框的一些操作數據的協議方法!
先來個,特寫,把x協議的家庭成員列出來:
@protocolUISearchBarDelegate
@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 *)searchBarshouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
-(void)searchBarSearchButtonClicked:(UISearchBar*)searchBar;
-(void)searchBarBookmarkButtonClicked:(UISearchBar*)searchBar;
-(void)searchBarCancelButtonClicked:(UISearchBar*) searchBar;
-(void)searchBarResultsListButtonClicked:(UISearchBar*)searchBar;
-(void)searchBar:(UISearchBar*)searchBarselectedScopeButtonIndexDidChange:(NSInteger)selectedScope;
@end
這不需要解釋吧,看方法名稱就能瞭解!
我們來看一看,常用的委託方法吧。
#pragma mark - UISearchBarDelegate 協議
//UISearchBar得到焦點並開始編輯時,執行該方法
-(BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar{
[self.searchBar setShowsCancelButton:YESanimated:YES];
[self.navigationControllersetNavigationBarHidden:YESanimated:YES];
[selfcontrolAccessoryView:0.9];//顯示遮蓋層。
return YES;
}
//取消按鈕被按下時,執行的方法
-(void)searchBarCancelButtonClicked:(UISearchBar*)searchBar{
[self.searchBar resignFirstResponder];
[self.searchBar setShowsCancelButton:NOanimated:YES];
[liveViewAreaTablesearchDataBySearchString:nil];//搜索tableView數據
[self.navigationControllersetNavigationBarHidden:NOanimated:YES];
[selfcontrolAccessoryView:0];//隱藏遮蓋層。
}
//鍵盤中,搜索按鈕被按下,執行的方法
-(void)searchBarSearchButtonClicked:(UISearchBar*)searchBar{
NSLog(@"---%@",searchBar.text);
[self.searchBar resignFirstResponder];//放棄第一響應者
[liveViewAreaTablesearchDataBySearchString:searchBar.text];
[self.navigationControllersetNavigationBarHidden:NOanimated:YES];
[selfcontrolAccessoryView:0];//隱藏遮蓋層。
}
//當搜索內容變化時,執行該方法。很有用,可以實現時實搜索
-(void)searchBar:(UISearchBar *)searchBartextDidChange:(NSString*)searchText;{
NSLog(@"textDidChange---%@",searchBar.text);
[liveViewAreaTablesearchDataBySearchString:searchBar.text];//搜索tableView數據
[selfcontrolAccessoryView:0];//隱藏遮蓋層。
}
3.遍歷UISearchBar控件的子控件,這樣可以針對不同的子視圖來設置外觀了。
for(id subViewin [self.searchBarsubviews]){
if([subViewisKindOfClass:[UIButtonclass]]){
UIButton *btn = (UIButton *)subView;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}