我试图使用UISearchController在地图视图上搜索目的地.我希望UISearchBar出现在导航栏中,但我似乎无法在没有显示右侧的取消按钮的情况下这样做:

这个取消按钮有时会消失,而我正在玩耍,但我现在无法让它显示我的搜索表显示我想要它:

我肯定必须有一些我做得有点小错误的东西,但我无法弄清楚它是什么......
self.resultsViewController = [UITableViewController new];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsViewController];
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = false;
self.searchController.delegate = self;
self.searchBar = self.searchController.searchBar;
self.searchBar.placeholder = self.stage.title;
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.definesPresentationContext = true;
self.navigationItem.titleView = self.searchBar;
self.resultsTableView = self.resultsViewController.tableView;
self.resultsTableView.dataSource = self;
self.resultsTableView.delegate = self;
Run Code Online (Sandbox Code Playgroud) 该文档UISearchController说明您可以覆盖- searchBar以提供UISearchBar控制器使用的自定义子类.确实使用了自定义搜索栏,并且正确调用了自己的委托方法,但UISearchResultsUpdating在搜索栏更改时不再调用该方法.我是否需要手动进行大量的布线操作,或者是否有一些我缺少的东西让控制器的行为与本机提供的搜索栏一样?
我有一个带有搜索栏的tableview.搜索栏由UISearchController提供.当我将搜索栏添加到表的标题视图时,表的第一行将被搜索栏覆盖.
如何防止搜索栏隐藏第一行?
我在viewDidLoad中有这个片段:
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
Run Code Online (Sandbox Code Playgroud) 我在与UISearchController关联的UINavigationItem的titleView中有一个UISearchBar.当我向后导航时,它似乎闪烁.有人见过这个吗?
@interface HNTileSearchViewController () <HNTileSearchResultsProtocol, SWRevealViewControllerDelegate, UISearchBarDelegate, HNSetSearchFiltersProtocol, HNKeywordResultsProtocol>
...
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UISearchBar * searchBarTop;
...
@end
@implementation HNTileSearchViewController
...
- (void) customPreSetup {
HNKeywordResultsTableViewController * searchResultsController = [self.storyboard instantiateViewControllerWithIdentifier:HNKeywordResultsTableViewControllerStoryboardIdentifier];
searchResultsController.delegate = self;
_searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
_searchController.searchResultsUpdater = searchResultsController;
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchBarTop = _searchController.searchBar;
_searchBarTop.delegate = self;
_searchBarTop.placeholder = NSLocalizedString(@"Search heynay", nil);
_searchBarTop.showsCancelButton = NO;
_searchBarTop.showsScopeBar = NO;
self.navigationItem.titleView = _searchBarTop;
self.definesPresentationContext = YES;
}
- (void) …Run Code Online (Sandbox Code Playgroud) 我的项目正在使用ContactsiOS 9中引入的新框架
我的几个UITableView子类调用了一个CNContactPicker,我想使用顶部显示的搜索栏.
这些视图中的一些只是实现委托方法 - contactPicker:didSelectContact:另一个实现了该方法以及 - contactPicker:didSelectContacts:,其实现导致CNContactPicker允许多个选择.
我让各个类声明它们是,UISearchControllerDelegates并且很高兴看到只需要使用我的contactPicker委托方法进行选择.
不幸的是,第三个视图不起作用 - 您在搜索字段中键入内容,它会根据您键入的内容提供过滤器列表,但是当您点击选择时,选择会突出显示但不会发生任何其他情况.如果我回到代码并注释掉contactPicker:didSelectContacts:方法然后它工作,
据推测,有一种方法可以使此响应,否则CNContactPicker当允许多个选择时,视图将不会显示搜索栏.
我应该怎么做才能使这项工作?
搜索栏正好显示为64点太低:
所有其他帧都完全正确.
编辑: - 这UISearchController是错误的观点origin.y.它设置为64,当它应该为0.如果我添加此方法:
- (void)didPresentSearchController:(UISearchController *)searchController
{
[super didPresentSearchController:searchController];
searchController.view.frame = CGRectMake(0, 0, searchController.view.frame.size.width, searchController.view.frame.size.height);
}
Run Code Online (Sandbox Code Playgroud)
然后视图对齐.然而,它的janky因为它跳跃.如果我在willPresentSearchController其中修改框架不起作用,因为控制器必须在其呈现后进行某种布局.
如果我使用SparkInspector,并编辑UISearchBarContainerViewfrom origin 64 的框架(它设置为0,为0),问题就解决了.
这是我的相关配置:
self.searchResultsController = [[GMSearchTableViewController alloc] init];
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
[self.view addSubview:self.searchController.searchBar];
Run Code Online (Sandbox Code Playgroud)
我没有使用Interface Builder,一切都是在代码中配置的.我很肯定,设置definesPresentationContext是正确的.
VC位于一个常规的UINavigationController,它位于SplitViewController内部(但iPhone上也存在问题).
我觉得我错过了一个简单的配置选项 UINavigationBar
我还有一个不同的控制器,它使用一个更复杂的自定义容器视图控制器模型,并且可以使用.
当我设置
self.definesPresentationContext = NO;
Run Code Online (Sandbox Code Playgroud)
所以现在UISearchBar获取正确,但是表示上下文是错误的,导致UISearchController表视图占据整个视图.
在iOS 11中,Apple提供了新的搜索栏,但是在iOS 11.2.2开发人员中,当两个视图控制器都有searchController时,它们会推送\ pop动画错误.Apple在Files app中展示了他们的愿景.有人知道怎么做吗?

编辑#1
动画破碎的问题. 破碎的UIearchBar动画嵌入在NavigationItem中
search uinavigationcontroller swift uisearchcontroller ios11
我正在从具有大样式UINavigationItem的视图转换为具有大样式UINavigationItem和视图的视图UISearchController.我已经定制了背景颜色UINavigationBar.
出于某种原因,在UINavigationBar和之间有1px的线或空间UISearchController.我可以告诉它NavigationBar,因为如果我滚动以便搜索栏粘在顶部,那么该线就会消失.我只是不确定是什么产生了这条线.我没有使用阴影,或任何花哨的东西,除了设置barTintColor和tintColor.如果我不使用大型标题,我会看到该行,但仅限于在视图之间转换时.这就像过渡时UISearchController刚刚过去的那样UINavigationBar.
任何帮助确定这条线的来源是值得赞赏的.
更新:在进一步的调查中,似乎只有在navigationItem.hidesSearchBarWhenScrolling设置为时才会发生false.如果它最初是隐藏的,那么前一个视图中的动画是平滑的,并且显示条形也是平滑的.
我已经使用 Xcode 11 (iOS 13) 在故事板中定义了 2 个视图控制器的示例项目中对此进行了测试。“呈现”视图控制器嵌入在导航控制器中,并在viewWillAppear. “搜索”视图控制器增加了一个UISearchController在viewDidLoad与由所述呈现视图控制器(未模态)推动。
仅在显示搜索视图控制器时进行此设置,导航栏将具有预期的蓝色背景和红色色调。但是,当向下滚动并显示搜索栏时,导航栏的背景颜色会丢失(或更改为默认的 iOS 灰色/半透明)。但是,如果您向上滚动(隐藏搜索栏)或关注搜索栏文本字段,导航栏颜色会返回!
此外,如果您专注于搜索栏文本字段,然后取消(通过点击取消按钮)导航栏的色调将从红色恢复为默认的 iOS 蓝色,这可以通过后栏项目注意到。
有关解决此问题的任何建议?
我也在viewWillAppear搜索控制器的 中设置了导航栏颜色,这并没有改变这种行为。
我为搜索控制器中的导航栏设置isTranslucent了true,这似乎阻止了背景颜色的恢复,但它并没有改变取消时色调颜色的恢复。
呈现视图控制器
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.tintColor = .red
}
Run Code Online (Sandbox Code Playgroud)
搜索视图控制器
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Search VC"
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.searchController = searchController
definesPresentationContext = true
}
Run Code Online (Sandbox Code Playgroud)
编辑
除了默认为 iOS 蓝色的系统栏按钮外,按照建议设置scrollEdgeAppearance,backButtonAppearance和buttonAppearance是一种享受。这可以通过设置 UINavigationBar.tintColor …
ios ×5
uisearchbar ×5
objective-c ×3
swift ×3
uitableview ×2
cncontact ×1
ios11 ×1
ios8 ×1
ios9 ×1
search ×1
uikit ×1