2014-09-29 37 views
2

我有一個UIViewController,它有分段控制和UITableView。 所有使用Autolayout,在故事板中設置。這裏的約束代碼軟件i設置有:強制UISearchController顯示搜索結果表全屏

H:|-[SegmentedControls]-| 
H:|[TableView]| 
V:|-[SegmentedControls]-[TableView]| 

我已經加入UISearchController和作爲的UISearchBar的表頭視圖。 爲了顯示搜索結果,我創建了一個新的UITableViewController。

UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain]; 
searchResultsController.tableView.dataSource = self; 
searchResultsController.tableView.delegate = self; 

self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController]; 
self.searchController.delegate = self; 
self.searchController.searchResultsUpdater = self; 
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0); 
self.clientListTable.tableHeaderView = self.searchController.searchBar; 
self.searchController.searchBar.scopeButtonTitles = @[@"Active", @"Inactive"]; 
self.definesPresentationContext = YES; 

但後來我遇到了以下問題 - 當我按下搜索欄上,它的動畫,但所提出的觀點需要,而不是隻在屏幕的一部分,呈現在頂部和底部的暗淡的鉻,呈現全屏。 呈現的視圖的大小似乎等於其中一個託管搜索欄的tableview,它只是垂直居中在屏幕上。 我對如何重寫搜索結果的呈現毫無頭緒,以使它們全屏顯示。我試圖明確地設置ModalPresentationStyle兩個呈現視圖控制器和一個正在呈現,但它沒有奏效。 我感到我需要以某種方式覆蓋搜索結果的表示控制器,但我不知道從哪裏開始,有什麼想法?

+0

你看過Apple的UICatalog示例代碼嗎? https://developer.apple.com/library/ios/samplecode/UICatalog/Introduction/Intro.html – 2015-08-29 01:45:49

+0

這是我做的第一件事之一,下一個要求在蘋果開發論壇和蘋果開發支持 – 2015-08-29 13:40:13

回答

0

經過約一年的使用變通方法(手動管理來自UISearchBar的輸入,沒有UISearchController),我找到了一個解決方案。 SearchController將結果顯示在一個新表中,該表被設置爲與從其調用的表相同的大小。所以,因爲我的桌子只是視圖的一部分,所提供的桌子也不是全屏。 我錯了,是我試圖隱藏分段控件,但我沒有更新約束(當時自動佈局仍然不是很好)。所以看的「自動佈局之謎」今年的WWDC演講後,我想出了一個解決方案:

-(void)willPresentSearchController:(UISearchController *)searchController { 

[UIView animateWithDuration:0.5 
         delay:0.0 
        options: UIViewAnimationOptionCurveEaseInOut 
       animations:^(void) { 
        [NSLayoutConstraint deactivateConstraints:@[_constraintToTheSegmentalControls,_constraintToTheTop]]; 
        [NSLayoutConstraint activateConstraints:@[_constraintToTheTop]]; 
        self.segmentedControls.hidden = YES; 
       } 
       completion:NULL]; 
} 

    -(void)didDismissSearchController:(UISearchController *)searchController { 

[UIView animateWithDuration:0.5 
         delay:0.0 
        options: UIViewAnimationOptionCurveEaseInOut 
       animations:^(void) { 
        [NSLayoutConstraint deactivateConstraints:@[_constraintToTheSegmentalControls,_constraintToTheTop]]; 
        [NSLayoutConstraint activateConstraints:@[_constraintToTheSegmentalControls]]; 
        self.segmentedControls.hidden = NO; 
       } 
       completion:NULL]; 
} 

約束「constraintToTheSegmentalControls」是從表視圖頂部的分段控制的底部約束,由默認是活動的。 約束「constraintToTheTop」是從表視圖頂部到頂部佈局指南的約束,默認情況下是無效的。 我將它們都作爲IB引用添加到了我的視圖控制器。

動畫仍然需要一些調整,但解決方案正在工作。