我有一个适用于 iOS8 的 iPhone 应用程序。UIPageViewController 有一个 UITableViewController 的子视图,这就是我放置 UISearchController 的地方。
当 UITableViewController 第一次出现时,我可以通过以下方式激活搜索
[self.searchController.searchBar becomeFirstResponder];
但是,一旦视图控制器通过推送到另一个表视图控制器或模态呈现 UIViewController(视觉上屏蔽它)变得不可见,searchBar 在重新出现后就不能再成为第一响应者。
搜索本身可以通过
[self.searchController setActive:YES];
但是,没有插入符号以这种方式闪烁(用户必须在 searchBar 内点击才能开始输入)。
我有几乎相同的 UITableViewController 和 UISearchController 没有事件。所以,我一定是遗漏了一些东西,但我想请你对我在解决这个问题时应该注意哪个方向提出意见。
下面是我如何设置搜索控制器。
@interface MonthTableViewController () <UISearchResultsUpdating, UISearchControllerDelegate>
@property (nonatomic, strong) UISearchController *searchController;
UITableViewController *searchController = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
searchController.tableView.dataSource = self;
searchController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchController];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = 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.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
// Hide search bar
CGRect myBounds = self.tableView.bounds;
myBounds.origin.y += self.searchController.searchBar.frame.size.height;
_lastlyAddedTableBoundY = self.searchController.searchBar.frame.size.height;
self.tableView.bounds = myBounds;