0

自動演示UISearchController和我自己提供的搜索控​​制器之間的行爲不同。UISearchController不會在前面但在UISearchController下面推視圖控制器

@implementation MyViewComtroller 

// click search barbutton on right of navigationBar 
- (void)searchAction:(id)sender { 
    ArticleSearchViewController *searchResultsController = [[ArticleSearchViewController alloc] init]; 
    UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController]; 
    searchController.searchResultsUpdater = searchResultsController; 
    searchController.searchBar.delegate = searchResultsController; 
    searchController.delegate = searchResultsController; 
    searchController.hidesNavigationBarDuringPresentation = NO; 
    [self presentViewController:searchController animated:YES completion:nil]; 
} 

@end 

@implementation ArticleSearchViewController 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    ArticleDetailController * articleDetailController = [ArticleDetailController new]; 
    [(UINavigationController *)self.presentingViewController pushViewController:articleDetailController animated:YES]; 
} 

@end 

推動動作發生在UISearchController下面。

+0

如果爲SearchResultsControl設置了一個UINavigationController ler,推動動作發生在UISearchController上,但searchBar不能隱藏動畫。 – weiminghuaa

回答

1

您以錯誤的方式呈現搜索控制器。看看部分文檔。

您可以使用搜索控制器與您現有的視圖 控制器相結合。當你有一個帶有可搜索內容的視圖控制器時, 將UISearchController對象的搜索欄合併到你的視圖控制器的界面中。當用戶與該搜索欄進行交互時,搜索控制器會自動在您指定的搜索結果中顯示新的視圖控制器 。

[self presentViewController:searchController animated:YES completion:nil]; 

你應該這樣做iOS上的11:

self.definesPresentationContext = YES; 
self.navigationItem.searchController = searchController; 

還是這個預iOS的11:

self.definesPresentationContext = YES; 
self.navigationItem.titleView = searchController.searchBar; 

在主控制器

取而代之的是或放置在您展示的其他地方searchController.searchBar視圖層次。

您還應該在viewDidLoad這樣的地方設置搜索控制器,而不是按下按鈕。

而且你得到一個關於你navigationControllerArticleSearchViewController必須進行更新,以類似的方式:

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.view setBackgroundColor:[UIColor blackColor]]; 

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    ArticleSearchViewController *searchResultsController = [sb instantiateViewControllerWithIdentifier:@"ArticleSearchViewController"]; 
    UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController]; 
    searchController.searchResultsUpdater = searchResultsController; 
    searchController.searchBar.delegate = searchResultsController; 
    searchController.delegate = searchResultsController; 
    searchController.hidesNavigationBarDuringPresentation = NO; 
// [self presentViewController:searchController animated:YES completion:nil]; //WRONG 
    self.definesPresentationContext = YES; 
    self.navigationItem.searchController = searchController; 
} 

@end 

結果控制器:與搜索內容

[self.presentingViewController.navigationController pushViewController:articleDetailController animated:YES]; 

視圖控制器

@implementation ArticleSearchViewController 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    UIViewController * articleDetailController = [UIViewController new]; 
    [self.presentingViewController.navigationController pushViewController:articleDetailController animated:YES]; 
} 

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController { 
    // update appropriately 
} 

@end 
相關問題