我想在自定义 UIViewController 中使用 UISearchDisplayController 和 MKMapView。
我在导航栏中显示 UISearchBar。
所有功能都适用于 iPhone,但在 iPad 上,即使在弹出窗口中,也不会显示结果的 TableView...我只想显示此 tableview...
(所有代表均已初始化良好)
通过调试,我知道TableView存在(searchDisplayController中有一个地址)
self.searchDisplayController.searchResultsTableView;
编辑:
除了在导航栏中插入搜索栏之外,我在 Interface Builder 中设置了所有内容。我只是以编程方式完成的:
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
我不明白,为什么在 iPhone 中显示 tableView 而在 iPad 中不显示!
编辑2:
如果我删除导航栏中的插入(上面引用的最后一行代码),则会显示 tableView,但以全屏显示,而不是在弹出窗口中。
最终编辑:
没有解决方案,我尝试了不同的方法:我将 View 嵌入到 SplitView 中,将 Research 嵌入到主视图中,将 MapView 嵌入到详细 View 中。它有效,但看起来与预期不同..
答案是:
使用 swift 和 iOS 8,我重建了我的应用程序,并找到了一个解决方案来很好地完成这项工作。
if let tableView = self.searchDisplayController?.searchResultsTableView {
tableView.frame = CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.height)
self.view.addSubview(self.blurEffectView!)
}
最佳答案
@Jeremy 感谢您的回答。我是 iOS 新手,您的回答帮助我解决了这个问题。这是一个可能对其他人有帮助的更新。
在 ViewDidLoad 中包含这段代码以保存原始 View 的副本
originalView = self.view;
为 searchDisplayController 添加以下两个委托(delegate)方法将解决您的问题。 如果您将 searchBar 放置在 NavigationBar 中,则使用
UIEdgeInsetsMake(70, 0, 0, 0)
否则使用UIEdgeInsetsZero
- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
if (OSVersionIsAtLeastiOS7() && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)) {
if (([originalView isEqual:self.view])) {
self.view = controller.searchResultsTableView;
controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(70, 0, 0, 0);
[self.searchDisplayController.searchBar becomeFirstResponder]; // This will retain the keyboard after keypress
}
}
}
要随时显示原始 View ,请使用 self.view = originalView;
在此委托(delegate)方法中,如果搜索文本为空,则显示原始 View 而不是黑屏。
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
if ([searchString isEqualToString:@""]) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
self.view = originalView;
}
}
else{
[self filterContentForSearch:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
}
return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
originalView.alpha = 0.5f;
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
originalView.alpha = 1.0f;
}
关于ios - UISearchDisplayController 在 iPad 上不显示结果 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23553388/