2014-09-29 80 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引用添加到了我的视图控制器。

动画仍然需要一些调整,但解决方案正在工作。