標籤:arc sel 表徵圖 方法 nss sid spel 文字框 hold
最近用到搜尋功能。於是,經過不斷的研究,終於,有點懂了。
那就來總結一下吧,好記性不如爛筆頭!
搜尋,無疑可以使用UISearchBar控制項!那就先瞭解一下UISearchBar控制項吧!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:[NSArray arrayWithObjects:@"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];// 是否提供自動修正功能,一般設定為UITextAutocorrectionTypeDefaultself.searchBar.delegate = self;// 設定代理[self.searchBar sizeToFit];myTableView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0);[self.view addSubview:myTableView];[myTableView addSubview:self.searchBar];
這麼多屬性,其實看起來多,你實際去操作事件一下,就發現很簡單的!絕大多部分都是定義一些外觀的東西!瞭解了各個屬性,一定能滿足你設計出你想要的外觀效果!!然後,解釋一下,我個人覺的比較有趣和重要的屬性!
[email protected] (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]; } }];}
[email protected](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]; } }
當然,不是很全。也是入門的層級,見效了!
希望對你有所協助!
iOS學習—— UISearchBar的使用