不能将 UISearchController 与 UICollectionView 一起使用? [英] Can't use UISearchController with UICollectionView?

查看:22
本文介绍了不能将 UISearchController 与 UICollectionView 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WWDC 2014 演讲A Look Inside Presentation Controllers"中,演示者展示了如何在 UITableView 中设置 UISearchController.他们通过设置 searchController 的 searchBar 的框架,然后将其设置为 tableView 的 tableHeaderView 来做到这一点.不幸的是,对于 UICollectionView,没有等效的 tableHeaderView.使用 UISearchDisplayController,这很简单:创建一个 UISearchBar 并将其添加到自定义 UICollectionView 部分标题,然后使用搜索栏初始化 UISearchDisplayController.问题是,您无法使用 UISearchBar 初始化 UISearchController,甚至无法设置 searchBar,因为它是只读属性.我想我真正的问题是,我有什么选择?没有 UISearchDisplayController 或 UISearchController 有没有好"的方法来实现搜索?

In the WWDC 2014 talk " A Look Inside Presentation Controllers" the presenters showed how to setup a UISearchController in a UITableView. They do this by setting the searchController's searchBar's frame, then setting it as the tableView's tableHeaderView. Unfortunately, there isn't an equivalent of tableHeaderView for UICollectionView. With UISearchDisplayController, this would be simple: create a UISearchBar and add it to a custom UICollectionView section header, then initialize the UISearchDisplayController with the search bar. The problem is, you can't initialize a UISearchController with a UISearchBar, or even set the searchBar because it's a read-only property. I guess my real question is, what are my options? Is there a "good" way to implement search without UISearchDisplayController or UISearchController?

推荐答案

使用 UISearchDisplayController,这很简单:创建一个 UISearchBar 并将其添加到自定义 UICollectionView 部分标题,然后使用搜索栏初始化 UISearchDisplayController

With UISearchDisplayController, this would be simple: create a UISearchBar and add it to a custom UICollectionView section header, then initialize the UISearchDisplayController with the search bar

UISearchController 中的搜索栏是为您创建的.当你被问到数据源方法中的补充视图时

The search bar in a UISearchController is created for you. When you are asked the supplementary view in the data source method

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

添加 searchController.searchBar 作为补充视图的子视图.别忘了打电话

add the searchController.searchBar as a subview of the supplementary view. Don't forget to call

[searchController.searchBar sizeToFit]

为了给搜索栏合适的大小.

in order to give the search bar the appropriate size.

这篇关于不能将 UISearchController 与 UICollectionView 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆