2015-11-18 6 views
7

私はUISearchControllerとUITableViewを持っています。 UISearchController dimsBackgroundDuringPresentationは検索テキストが空の場合のみ

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
self.searchController.searchResultsUpdater = self; 
self.searchController.dimsBackgroundDuringPresentation = YES; 
[self.searchController.searchBar sizeToFit]; 
self.searchController.searchBar.delegate = self; 
self.searchController.delegate = self; 

self.tableView.tableHeaderView = self.searchController.searchBar; 
self.tableView.userInteractionEnabled = YES; 

は私が検索バーをタップするたびグレービューが登場することにしたいと私は入力を開始するとき、グレー表示が消え、のtableViewはので、私はセルをタップすることができますを示しています。 のviewDidLoadでのコードです。 Thatsは、検索バーが空の場合にのみ灰色の表示が表示されることを意味します(メールと連絡先アプリのデフォルトの検索動作と同様)。私はsearchBar.text

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 

に基づいてデリゲートメソッドで

self.searchController.dimsBackgroundDuringPresentation 

を設定しようとしましたが、それは動作しません。 アイデア

ありがとう、

+0

これは、dimsBackgroundDuringPresentationがどのように機能するかを示しています。あなたはどのようにuisearchcontrollerを実装していますか?それは検索結果を鈍らせるだけではありません。あなたはおそらく何かを提示していないようです。 – beyowulf

+0

私はUISearchControllerのために何をしたのですか?それ以外はすべてテーブルビューで実装されています。(void)updateSearchResultsForSearchController:(UISearchController *)searchController は、検索テキストに基づいてtablaビューをリロードします。私がする必要があることは他に何かありますか? – Missa

+1

残念ながら、それはそれより複雑です。テーブルビューのデータソースを検索し、検索結果を見つけてユーザーに提示する必要があります。あなたはここでそれを行う方法に関するチュートリアルを見つけることができます:http://www.jhof.me/simple-uisearchcontroller-implementation/ – beyowulf

答えて

0

テーブル表示時にサブビューを追加し、グレー色とアルファを設定しました。 Dismiss SearchControllerがサブビューを削除しました。私はdimプロパティをfalseに設定しました。 私のコードは以下のとおりです。 検索結果の表示に同じ表を使用しました。

// on header file 
UIView *dimView = null; 

//on .m file 

     // create DimView for SearchControl 
    - (void)showDimView 
    { 
     if(dimView == nil && self.searchController.active) 
     { 
      CGRect rcReplacementView = self.tableView.frame; 
      dimView = [[UIView alloc] initWithFrame:rcReplacementView]; 
      dimView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
      dimView.backgroundColor = [UIColor blackColor]; 
      dimView.alpha = 0.5; 
      [self.view addSubview:dimView]; 
      self.tableView.scrollEnabled = NO; 

      //tap event for hide seachcontroll 
      UITapGestureRecognizer *singleFingerTap = 
      [[UITapGestureRecognizer alloc] initWithTarget:self 
                action:@selector(handleSingleTap:)]; 
      [dimView addGestureRecognizer:singleFingerTap]; 
      [singleFingerTap release]; 
     } 
    } 

//close SearchController if Tap on view 
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer { 

    if(searchController.searchBar.text.length <= 0) 
    { 
     [self.searchController setActive:NO]; 
    } 
} 

// do something before the search controller is dismissed 
- (void)willDismissSearchController:(UISearchController *)searchController { 

    if(dimView != nil) 
    { 
     [dimView removeFromSuperview]; 
     dimView = nil; 
    } 
    self.tableView.scrollEnabled = YES; 
} 
+1

ありがとう、私はリンゴから標準実装を実装することを好む。 – Missa

1

self.searchController.dimsBackgroundDuringPresentation = YESあなたは結果([[UISearchController alloc] initWithSearchResultsController:nil])を表示するために現在のビューを使用しているあなたのコード内でsearchResultsControllerしかしための別のビューコントローラを使用している場合に便利です。

検索バーをタップするたびにグレー表示が表示され、入力を開始するとグレー表示が消えてtableViewが表示され、セルをタップできます。

これは、searchResultsControllerに別のView Controllerを使用している場合のデフォルト動作です。

+0

私はAppleがこの奇妙で無駄なエフェクトを例外によって無効にするべきだと思うか、ログ内の警告である可能性があります...あなたが現在の(nil)としてsearchResultsControllerを選択すると、dimsBackgroundDuringPresentationは決してYESにならないはずです。 – faviomob

関連する問題