在我的应用程序中,我使用UISearchController(iOS 8中的新增功能)来显示搜索结果.有没有办法显示一组默认结果,当用户点击searchBar而不输入任何内容时会显示这些结果?
我找到了一些关于这个主题的问题和答案,但他们都使用旧的UISearchDisplayController,我似乎无法使用UISearchController解决方案.
谢谢
我想将UISearchBar添加到已经具有标题视图的UITableView.当我尝试将搜索栏添加到现有的标题视图时,它一直有效,直到我点击它,此时我得到The view hierarchy is not prepared for the constraint,这似乎是因为搜索栏不是tableview的直接子视图,所以当UISearchController尝试更新时它不能的约束.
我找到的唯一方法是将表视图标题作为搜索栏,然后一切正常,但当然我丢失了标题视图中已有的其他视图.
我显然还是 Xcode 的新手。所以SeacrhDisplayController在 iOS 8 中已弃用,我不知道如何UIsearchController在 tableview上实现。
我用谷歌搜索过它,但我没有看到任何特定或明确的教程。
这是我的代码SearchDisplayController:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [categories count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"FirstTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if …Run Code Online (Sandbox Code Playgroud) 我想实现UISearchController,使用本教程一步一步:
使用MKLocalSearchRequest搜索地点并使用UISearchController显示结果
在本节中......
...我在步骤3中遇到问题.设置表视图数据源
extension LocationSearchTable {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return matchingItems.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
cell.detailTextLabel?.text = ""
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试override从cellForRowAtindexPathtableView函数中删除,在这种情况下我在Xcode中没有错误,但执行后出错:
Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'UITableView (<UITableView: 0x7ff8e03a3800; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H;
gestureRecognizers = <NSArray: …Run Code Online (Sandbox Code Playgroud) 当我将另一个控制器推到堆栈上时(当有一个搜索控制器时)我在搜索栏下面有一个黑色块.我在下面的所有部分都将背景颜色设置为白色;
回到堆栈时也会出现.
func setUpSearchController() {
searchController.delegate = self
searchController.view.backgroundColor = .white
searchController.searchBar.backgroundColor = .white
searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = true
searchController.searchBar.sizeToFit()
searchController.searchBar.backgroundColor = .white
searchController.searchBar.barTintColor = .white
searchController.searchBar.placeholder = "Search"
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.tintColor = Colours.brandGreen
definesPresentationContext = true
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
} else {
messagesTableView.tableHeaderView = searchController.searchBar
}
}
Run Code Online (Sandbox Code Playgroud)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let chatController = ChatController() …Run Code Online (Sandbox Code Playgroud) 我正在试验UISearchController,但我做对了。我展示了UISearchController我自己的清洁UIViewController,它看起来很棒 - 但是一旦我旋转设备,它就会向上或向下移动几个点。
要重新创建它,只需执行以下几个步骤:
Main.storyboard从项目文件,并从项目设置中删除它的名字(项目- >常规- >目标- >主界面)在AppDelegate.swift:
application didFinishLaunchingWithOptions...{
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
Run Code Online (Sandbox Code Playgroud)
ViewController.swift:
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
let button = UIButton(frame: CGRect(x: 20, y: 200, width: 100, height: 40))
button.setTitle("Search", for: .normal)
button.backgroundColor = .black
button.addTarget(self, action: #selector(click), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func click(){
self.present(UISearchController(searchResultsController: nil), …Run Code Online (Sandbox Code Playgroud) 我正在使用UISearchControllerfor 搜索,UITableViewController它也支持通过UIRefreshControl.
在简化的 Demo 项目中设置非常简单
override func viewDidLoad() {
extendedLayoutIncludesOpaqueBars = true
title = searchTerm ?? "Search"
super.viewDidLoad()
setupSearch()
setupRefresh()
}
private func setupSearch() {
searchController.searchResultsUpdater = self
navigationItem.searchController = searchController
definesPresentationContext = true
//
// If this is set to `true` (which is also the default),
// UISearchBar and UIRefreshcontroll are buggy
//
navigationItem.hidesSearchBarWhenScrolling = true
}
private func setupRefresh() {
refreshControl = UIRefreshControl()
refreshControl?.addTarget(self, action: #selector(refresh), for: .valueChanged)
}
Run Code Online (Sandbox Code Playgroud)
这在 iOS 12 …
在iOS 8中点击UISearchController的搜索栏时,我无法获得响应事件。
我在视图中按下按钮时显示带有暗淡背景(如警报视图外观)的自定义视图,其中自定义视图具有UISearchController的搜索栏作为子视图。以编程方式添加了searchController。当我尝试触摸搜索栏时,它在iOS 8中没有响应,而我使用iOS 7的UISearchDisplayController添加了搜索栏,则工作正常。
我有带导航栏的viewControllerA。在viewController A中,我有一个按钮。在按下按钮时,我试图将视图显示为自定义视图,并带有暗淡的背景,如警报视图外观。所以我在按钮方法中添加了以下行。
class_name *view = [[class_name alloc] initWithNibName:@"xib_name" bundle:nil];
[self.navigationController.view addSubview:view];
Run Code Online (Sandbox Code Playgroud)
class_name的Xib文件背景较暗,带有子视图,如searchController的搜索栏。
代替
[self.navigationController.view addSubview:view];
Run Code Online (Sandbox Code Playgroud)
添加
[self presentViewController:view animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
UISearchController的搜索栏现在正在工作,但是我无法获得透明的背景视图,而是出现了黑色背景。
我需要显示以前的视图内容,并在当前视图的内容之上显示警报视图。
请在目标C中指导我。
在viewDidLoad我的自定义子类的UITableViewController,我已经设置navigationItem.titleView到searchBar的UISearchController,我与初始化nil了searchResultsController。
当我点击搜索栏时,输入文本,然后点击暗淡的基础内容,然后UISearchController将停用(按预期方式),并且清除输入的搜索文字(出乎意料)。
当我明确地(以编程方式)将搜索控制器设置为非活动状态时,搜索文本也被清除(也意外地)。为了解决这个问题,我先存储了搜索文本,然后在关闭搜索控制器后将其重新设置。尽管很hack,但它可以工作。
let searchText = searchBar.text // #hack
searchController.isActive = false
searchBar.text = searchText // #hack
Run Code Online (Sandbox Code Playgroud)
对于自动关闭搜索控制器的情况,我尝试使用UISearchControllerDelegate方法进行类似的操作,即将搜索文本存储在中willDismissSearchController(_:)并将其重新设置为didDismissSearchController(_:)。但是,此解决方案仍显示已清除搜索文本,然后将其设置回之前的状态。
自动关闭搜索控制器后,如何无缝保留搜索栏文本?
ios ×7
swift ×4
uisearchbar ×4
objective-c ×2
uikit ×2
uitableview ×2
ios13 ×1
ios16 ×1
ios8 ×1
ipad ×1
mapkit ×1
searchbar ×1
styles ×1